所以我最近拿起了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