【发布时间】:2015-12-02 07:53:47
【问题描述】:
我有一个程序正在寻找一个总数以及链表中的中间数字是多少。我的问题是为什么它不打印出值?
代码如下:
int count(list values){
if(values == NULL)
return 0;
else
return 1 + count(values->next);
}
void middle(struct node *head){
int count = 0;
struct node *mid = head;
while (head != NULL){
if(count & 1)
mid = mid->next;
count++;
head = head->next;
}
}
void traverse(list values){
if(values->next)
printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle);
}
int main(int argc, char *argv[]){
FILE *input = stdin;
list values = readNumbers(input);
traverse(values);
return 0;
}
【问题讨论】:
-
请给我们完整的代码。
-
预期和实际输出是多少?
-
list readNumbers (FILE *file){ list values = NULL, first = NULL;诠释 c; while ((c = getc(file)) != EOF) { int i = 0; list new = (list) malloc(sizeof(struct node));如果(值 == NULL)值 = 新;其他{值->下一个=新;价值观=新的; } 值 = 新的; if (first == NULL) first = values;做{值->值[i++] = c; } while ((c = getc(file)) != '\n' && c != EOF);值->值[i] = '\0'; } 先返回; }
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。
标签: c count linked-list median