我想使用 C 将二叉树转换为数组。我尝试过但没有成功。
我的二叉树包含以下元素(预购)
4 3 5 10 8 7
Run Code Online (Sandbox Code Playgroud)
但我的数组包含(排序后)
4 4 5 7 8 10
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。我当前的代码如下所示:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct tree
{
int data;
struct tree *left;
struct tree *right;
}tree;
int AddToArray(tree *node, int arr[], int i);
tree *CreateNode(int data);
tree *Insert(tree *node, int data);
void PrintPreorder(tree *node);
int count(tree *node);
int compare(const void * a, const void * b);
//---------------------------------------------------------------------------
int main()
{
int i;
int size;
int *arr=NULL;
tree *root=NULL;
printf("***TEST …Run Code Online (Sandbox Code Playgroud) 在这段代码中:
#include<stdio.h>
int var=100;
int main()
{
extern int var; //Declaration not Definition
printf("%d\n",var);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
100被印刷,其是什么不正常的,但是,当声明从除去main()中,即使在当时正在使用全局定义.这是怎么回事?这取自K&R,其中说:
还必须在每个想要访问它的函数中声明(全局)变量.
我想使用NSClassFromString和NSSelectorFromString为类方法和选择器构建调用.我尝试以下列方式构建调用:
NSMethodSignature *signature;
NSInvocation *inv;
Class targetClass = NSClassFromString(@"NSString");
SEL selector = NSSelectorFromString(@"stringWithString:");
id arg = @"argument";
Method method = class_getInstanceMethod(targetClass, selector);
Run Code Online (Sandbox Code Playgroud)
//当我运行代码时,为什么方法为nil?
struct objc_method_description* desc = method_getDescription(method);
if (desc == NULL || desc->name == NULL){
return nil;
}
signature = [NSMethodSignature signatureWithObjCTypes:desc->types];
inv = [NSInvocation invocationWithMethodSignature:signature];
[inv setSelector:selector];
[inv setArgument:&arg atIndex:2];
[inv invoke];
__autoreleasing id returnObj;
[inv getReturnValue:&returnObj]; // get created object
Run Code Online (Sandbox Code Playgroud)
由于'方法'总是'无',因此这种方法不起作用.为什么?
通过调用上述代码执行类方法的正确方法是什么?
你能告诉我我的类构造函数有什么问题吗?码:
CVector::CVector (int size_)
{
if (size_ > 0)
{
this->size = size_;
this->data = new double[size];
for (int i = 0; i < size; i++)
{
(*this)(i) = i;
}
}
cout << "constructor end" << endl;
return;
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
tvector = CVector(6);
Run Code Online (Sandbox Code Playgroud)
"constructor end"输出后我收到访问冲突.
更新:构造函数调用不正确.运用
CVector tvector(6);
工作.
我需要以这种方式操作一个字符串:
如果字符是'+'或' - '或'/'或'*',则将它们移动到缓冲区的末尾,否则,移动到缓冲区的开头.
我的解决方案非常简单:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void mix_the_string(char ** buff, char ** string)
{
printf("The string which will be printed is : %s\n",*string);
int i = 0;
int j = strlen(*string) - 1;
while(i< strlen(*string))
{
if(*string[i] != '+' || *string[i] != '-' || *string[i] != '*' || *string[i] != '/')
{
printf("in the first if, i = %d, *string[i] = '%d'\n",i,(int)*string[i]);
*buff[i] = *string[i];
}
else
{
printf("in the second if, i = %d, …Run Code Online (Sandbox Code Playgroud) c ×3
arrays ×1
binary-tree ×1
c++ ×1
constructor ×1
extern ×1
objective-c ×1
preorder ×1
scope ×1
tree ×1