所以我最近拿起了C,並且我正在按照一個簡單的在線教程創建和打印鏈表。我已經跟着它一步一步,出於某種原因,教程中的人能夠列出他的列表,而我不是。這讓我瘋狂。當我構建並運行(使用CodeBlocks)時,什麼都沒有顯示出來。使用CodeBlocks以C打印鏈表
他正在使用一些其他的文本編輯器,也許是一個不同的編譯器,但我不能爲我的生活看到完全相同的代碼可以有兩種不同的行爲?任何人有任何想法?代碼如下:
struct Node {
int data;
struct Node *next;
};
struct List {
struct Node *head;
};
void pushList(struct List *linkedList, int value) {
if (linkedList->head == NULL) {
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
newNode->data = value;
linkedList->head = newNode;
} else {
struct Node *tNode = linkedList->head;
while (tNode->next != NULL) {
tNode = tNode->next;
}
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
newNode->data = value;
tNode->next = newNode;
}
}
void printList(struct List *linkedList) {
struct Node *tNode = linkedList->head;
while (tNode != NULL) {
printf("This node has a value of %d\n", tNode->data);
tNode = tNode->next;
}
}
int main() {
struct List newList = { 0 }; //This initializes to null
pushList(&newList, 200);
pushList(&newList, 300);
pushList(&newList, 400);
pushList(&newList, 500);
printList(&newList);
return 0;
}
適合我。你是否包含''stdio.h'和'stdlib.h'? –
'newNode-> data = value;' - >'newNode-> data = value; newNode-> next = NULL;' – BLUEPIXY