【发布时间】:2017-03-15 06:27:33
【问题描述】:
我是编程新手。我写了一个函数来扫描链表的输入。但它不起作用。谁能帮我找出问题所在?
ListNode *BuildList() {
char discard;
ListNode *list,*list2=NULL;
list = (ListNode*)malloc(sizeof(struct ListNode));
if ((scanf("%d%1[^\n]s", &list->val, &discard)) == 2) {
list->next = BuildList();
printf("%d ", list->next->val);
}
else
{
list->next = NULL;
}
return list;
}
而ListNode被定义为
struct ListNode {
int val;
ListNode *next;
};
谢谢!
【问题讨论】:
-
“不起作用”不是一个有用的问题陈述。请阅读此ericlippert.com/2014/03/05/how-to-debug-small-programs
-
1)
"%d%1[^\n]s"错误。 -
试试
if (scanf("%d", &list->val) == 1) { scanf("%*[^\n]"); scanf("%*c"); … -
@CoolGuy 目前输入的格式不清楚。
标签: c linked-list