typedef struct Node
{
int data;
struct Node *next;
}SLIST;
p = pHead->next; p为头链表的指针,也就是指向第二个节点,应该从用户输入的第二个数字打印才对。
为什么连链表头都打印了?
这个是看不清部分的代码
SLIST *Creat_SList()
{
//1 创建头结点并初始化
int data = 0;
SLIST *pHead = NULL, *pM = NULL, *pCur;
pHead = (SLIST *)malloc(sizeof(SLIST));
pHead->data = 0;
pHead->next = NULL;
//2循环创建结点,结点数据域中的数值从键盘输入,
//以-1作为输入结束标志
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);
//准备环境 让pCur指向pHead
pCur = pHead;
while(data != -1)
{
//不断的malloc新节点 并且数据域 赋值
pM = (SLIST *)malloc(sizeof(SLIST));
pM->data = data;
pM->next = NULL;
//1新节点入链表
pCur->next = pM;
//2 当前结点下移(新结点变成当前结点)
pCur = pM; // (pCur = pCur->next) pCur之前不是指向head吗?现在指向了PM.
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);
}
return pHead;
}
我链表头也记录数据了,它应该从第二个接点开始打印才对,为什么从链表头开始打印了?