该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
typedef struct Node
{
int data;
struct Node *next;
}SLIST;
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;
}
第一个scanf应该也记录对吧? 它传入date的地址了。
另外这个打印函数应该跳过头结点,直接打印第二个节点才对。
为什么也打印头节点了?
头节点应该被记录,不被打印才对。
结果它不光记录了,也打印了。