#include"stdio.h"
/*
連結清單是線性表的鍊式存儲
連結清單由一個一個結點組成
每一個節點分為指針域和資料域
指針域有一個的叫單連結清單 ,有兩個叫雙連結清單,三個及三個以上的叫多連結清單
結點的單詞叫“node”
struct node{
int data;//資料域存儲資料;
struct node *next; //指針域,指向下一個節點的位址
}
配置設定空間用malloc
釋放空間用free
計算位元組大小sizeof
建立連結清單三步驟
1:先給結點申請空間
2:給該結點指派(資料域指派)
3:将新結點連接配接到連結清單上
*/
struct node{
int data;
struct node *next;//因為next是一個要指向下一個資料域的指針是以類型為 struct node
};
void main()
{
int j;
struct node *head;//頭結點 決定後續輸出時的首個位址
struct node *q;//用q來不斷地建立新的空間
struct node *s;//負責連接配接
struct node *shuchu;//負責輸出
for(j=0;j<5;j++)
{
q=(struct node *)malloc(sizeof(struct node));
printf("第%d個:",j+1);
scanf("%d",&q->data);
q->next=NULL;
if(j==0)
{
head=q;//輸出時備用 可以看作時首位址
s=q;
}
else
{
s->next=q;
s=q;
}
}
shuchu=head;
for(j=0;j<5;j++)
{
printf("%d",shuchu->data);
shuchu=shuchu->next;
}
}