連結串列學習(一)靜態連結串列的構造
阿新 • • 發佈:2018-12-29
#include<stdio.h>
typedef struct Student //宣告結構體型別 struct Student
{
int num;
float score;
struct Student *next;
} stu ;
int main()
{
//定義5個結構體變數,a,b,c作為連結串列的節點,
//定義head和p 是指向struct Student 結構體型資料的指標變數
stu a,b,c,*head,*p;
//對節點a,b,c的成員進行賦值
a.num=10101;a.score=89.5;
b.num=10103;b.score=90;
c.num=10107;c.score=85;
//將a的起始地址賦給頭指標head
head=&a;
//將b的起始地址賦給a節點的next成員
a.next=&b;
//將c的起始地址賦給b節點的next成員
b.next=&c;
//c節點的next成員不存放其他節點地址
c.next=NULL;
//使p指向節點a
p=head;
do
//輸出p指向節點的資料
{ printf("%1d%5.1f\n",p->num,p->score);
//使p指向下一個節點
p=p->next;
} while(p!=NULL);//輸出完最後一個節點後,迴圈終止
return 0;
}
10101 89.5
10103 90.0
10107 85.0