1
我是一個編程新手。我寫了一個函數來掃描輸入到鏈表。但它不起作用。任何人都可以幫我找到問題所在嗎?掃描輸入到鏈接列表
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;
};
謝謝!
歡迎來到Stack Overflow! [請參閱此討論,爲什麼不在'C'中投射'malloc()'和family的返回值。](http://stackoverflow.com/q/605845/2173917)。 –
「不起作用」不是一個有用的問題陳述。請閱讀https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – StoryTeller
1)''%d%1 [^ \ n] s「'是錯誤的。 – BLUEPIXY