问题遇到的现象和发生背景
在学习链表的时候,使用头插法遇到了问题。
问题相关代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
struct Node {
int date;
struct Node* next;
};
/*创建链表头 /
struct Node createList()
{
struct Node headNode = (struct Node)malloc(sizeof(struct Node));
headNode->date = 1;
headNode->next = NULL;
return headNode;
}
/创建节点/
struct Node* createNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->date = data;
newNode->next = NULL;
return newNode;
}
/打印链表/
void printList(struct Node* headNode)
{
struct Node* pmove;
pmove = headNode->next;
while (pmove)
{
printf("%d", pmove->date);
pmove = pmove->next;
}
printf("\n");
}
/插入节点,插入哪个链表,插入节点的数据是多少,头插法/
void inserNodebyHead(struct Node* headNode, int data)
{
/创建新的节点/
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
//newNode = headNode->next; ** //使用这一句代码时,不能正常打印出链表的成员,而下面那句代码却可以**
headNode->next = newNode;
}
int main()
{
struct Node* list = createList();
inserNodebyHead(list, 1);
inserNodebyHead(list, 2);
inserNodebyHead(list, 3);
printList(list);
return 0;
}
/正确运行的输出结果是321,而使用那句问题代码时,却什么都没有打印,好想知道问题出在哪/