1. 程式人生 > >簡單靜態連結串列

簡單靜態連結串列

下面的程式碼是一個簡單的靜態連結串列,它是由3個學生的資料組成的(學號,成績)的結點組成。

#include<iostream>
#include<stdio.h>
using namespace std;

struct student
{
    long num;
    float score;
    struct student * next;          //該指標指向student型別的結構體
};                                   //必須有分號

int main()
{
    struct student a,b,c,*head,*p;
    a.num=16010; a.score=89.5;       //賦值
    b.num=16011; b.score=98;
    c.num=16012; c.score=94;
    head=&a;                         //將a的地址給head
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;
    do                               //輸出記錄
    {
        cout<<p->num<<" "<<p->score<<endl;
        p=p->next;
    }while(p!=NULL);
    getchar();
}