# include<stdio.h>
# include<stdlib.h>
using namespace std;
typedef struct node
{
int data;
struct node * next;
}Node;
//尾插法,需要引入第三方變量,保證頭指針起到定海神針的作用,最後將頭結點的指針返回給頭指針
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 = cur;//邏輯上只能提供這麼多想法,但這種書寫方式比較易記,我也就暫時先這樣記憶
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;
}