# include<stdio.h>
# include<stdlib.h>
typedef struct node
{
int data;
struct node * next;
}Node;
//尾插法,需要引入第三方变量,保证头指针起到定海神针的作用,最后将头结点的指针返回给头指针
//整体思想:创建一个时刻指向新来的节点的指针Pt, 并利用Pt更改每次尾结点中NULL的指向
Node * creat_list_end()
{
Node * head = (Node*)malloc(sizeof(Node));
head->next = NULL;
Node * cur = NULL;
Node * pt = head;
int data;
printf("请输入节点数据:\n");
scanf("%d", &data);
while (data)
{
cur = (Node *)malloc(sizeof(Node));
cur->data = data;
cur->next = NULL;
pt->next = cur;//利用Pt更改每次尾结点中NULL的指向
pt = cur;//时刻指向新来的节点的指针Pt
scanf("%d", &data);
}
return head;
}
void show_list(Node * head)
{
Node * phead = head->next;
while (phead)
{
printf("%d\n", phead->data);
phead = phead->next;
}
}
int main()
{
Node * head = (Node *)malloc(sizeof(Node));
head = creat_list_end();
show_list(head);
return 0;
}