typedef struct Node
{
int data;
struct Node *next;
}SLIST;
SLIST *Creat_SList();
int SList_Print(SLIST *pHead);
//在结点数值为x的前面插入y
int SList_NodeInsert(SLIST *pHead, int x, int y);
//删除结点为y的链表结点
int SList_NodeDel(SLIST *pHead, int y);
int SList_Destory(SLIST *pHead);
SLIST *Creat_SList()
{
//1 创建头结点并初始化
int data = 0;
SLIST *pHead = NULL, *pM = NULL, *pCur = NULL;
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) //回想创建的内存模型你立刻就知道了。
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);
}
return pHead;
}
上面是创建链表代码,下面是打印链表代码。
int SList_Print(SLIST *pHead)
{
SLIST *p = NULL;
if (pHead == NULL)
{
return -1;
}
//准备环境
p = pHead->next;
printf("\nBegin ");
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("End ");
return 0;
}
创建链表中,头结点也记录了用户输入,打印链表的代码,我看应该不打印头结点数据,应该从第二个节点开始打印,为什么这打印代码也从头结点开始打印了?
麻烦各位看一下。