c编程如何从树中获取叶子列表

我有一个二叉树,我需要列出所有的叶子 树定义是: typedef struct tree { TreeNode* root; List leafList; } Tree; treeNode定义: typedef struct treeNode { int data; struct treeNode* parent; struct treeNode* left; struct treeNode* right; } TreeNode; 列表定义 typedef struct list { ListNode* head; ListNode* tail; }列表; listNode定义 typedef struct listNode { int data; struct listNode* next; } ListNode; 在我的第一步,我建立了树(很好)。 现在,在我的第二步,我需要从所有叶子列出一个列表。 对于前 […]

C初学者 – 将char *数组复制到另一个char *数组

我一直在挣扎这个愚蠢的时间。 基本上,我需要将一个char指针数组复制到另一个char指针数组。 现在,我有这个function: void copyArray(char *source[], char *destination[]) { int i = 0; do { destination[i] = malloc(strlen(source[i])); memcpy(destination[i], source[i], strlen(source[i])); } while(source[i++] != NULL); } 这导致分段错误。 有人可以帮忙吗? 谢谢! 编辑:示例程序 #include #include #include // Copy the contents of one array into another void copyArray(char *source[], char *destination[]){ // printf(“In copy array”); int i = 0; […]

C – 对这种奇怪的行为感到茫然

所以…我有一小段代码,我早上大部分时间都在做。 这只是一个小项目,可以帮助我记住语法等等。 我显然错过了某种大错误,因为代码因为我不理解的原因返回了分段错误。 #include #include #include #include #include struct cards { int card_value[99]; char card_name[99]; char card_suit[99]; int card_tally; }; struct cards hand[2]; void tally (int a) { int k, j; for (k=0; k5; d++) { if (hand[a].card_name[d] ==’A’) { int y; int z = 10; for (y=0; y<5; y++) z = z + hand[a].card_value[y]; […]

用strtok分割故障

#include #include int main(){ char path[] = “/fs/lost+found”; char* temp; temp = strtok(path,”lost+found”); while(temp != NULL){ printf(“\n %s \n”,temp); temp = strtok(path,”lost+found”); } return 0; } 我想提取丢失+找到的字符串。 上面的程序进入无限循环并打印分隔符“lost + found”之前的“/” [root @ rs]#。/ a.out分段错误

如何计算文件中的数字?

这是错误的。 我的代码出了什么问题? #include “stdafx.h” #include “stdlib.h” #include “ctype.h” int _tmain(int argc, _TCHAR* argv[]) { FILE* input; int num; int numCount = 0; input = fopen(“123.txt”, “r”); if (!input) { printf(“No file \a\n”); exit (101); } while ((fscanf(input, “%d”, &num)) == 1) printf(“%d”, num); if (isdigit(input)) numCount++; printf(“number count: %d”, numCount); return 0; }

printf与scanf请求输入。 BST循环错误

我一直试图看看为什么printf在打印文件输入后没有打破循环。 .c文件是BST,我现在正在测试是否已经构建了一个树,但似乎无法退出printf循环。 我需要printf循环来获得代码的正确输出。 有关此错误发生原因的任何建议。 显示完整的代码和输出。 #include “bst.h” #include #include ///Author: Joe W //arbitrary list of temp nodes TreeNode *new_node, *root, *tmp, *parent; int elemArray[100], i1, i2, i0; /* Insert a new node into the tree by referencing the root and using recursion */ TreeNode* getN(int dataElem) { TreeNode* temp; temp = malloc(sizeof(TreeNode*)); temp-> left = […]

如何将c代码移植到java中? 开始说明

我正在努力将库从C ++移植到Java。 我想知道开始这个端口的第一个/初始步骤。 我不确定如何测试/调试? 我可以从“主”文件开始并开始重写代码,但是我如何以及何时测试我在做什么? 当我完成COMPLETE端口? 我该如何开始,任何帮助都会很棒。 任何跨平台/ etc编译器都会有帮助吗? 请告诉我相应的步骤

带有指向下一个结构的指针?

我想要一个会话,我在链接列表中的size变量中插入了10个不同的整数。 我相信我应该使用result_register()吗? 当存储所有10个整数时,我想通过输入result-> size打印出来。 我是链接列表的新手,所以请保持温和。 struct result { int size; }; struct result* result_next() { // What do I type here? } void result_register(int size) { // What do I type here? } 主要 struct result* result; while ((result = result_next())) printf(“%d\n”, result->size); 然后我希望能够通过如上所述打印出结果。

%dn是格式字符串吗?

我最近在代码中遇到了这一行 – fprintf(logfile,” |-IP Version : %dn”,(unsigned int)iph->version); 这里的“%dn”是格式字符串吗? 如果是这样,它意味着什么?

如何在C中随机生成0或1

我已经阅读了很多关于这个主题的post: rand()如何工作? 它有一定的倾向吗? 有没有比这更好用的东西? 随机数生成器如何在C中工作 这就是我得到的: 1)xn + 1取决于xn即生成的先前随机数。 2)不建议在程序中多次初始化种子。 3)使用rand()%2随机生成0或1是一种不好的做法。 我的问题是: 1)是否有任何其他库我错过了看看生成一个完全随机的数字(0或1)而不依赖于以前的输出? 2)如果还有其他工作使用内置的rand()函数来满足要求? 3)在程序中多次初始化种子的副作用是什么? 代码段: srand(time(NULL)); d1=rand()%2; d2=rand()%2; 在这里,我的目的是使d1和d2完全相互独立。 我最初的想法是这样做: srand(time(NULL)); d1=rand()%2; srand(time(NULL)); d2=rand()%2; 但正如我之前提到的基于其他post,我认为这是一种不好的做法? 那么,任何人都可以回答上述问题吗? 如果我完全错过了一件明显的事,我道歉。