首页 > 代码库 > C++链表-最简单

C++链表-最简单

struct student

  long num;

  float score;

  struct student *next;

};

注意:只是定义了一个struct student类型,并未实际分配存储空间。只有定义了变量才分配内存单元。

#include<iostream>using namespace std;int main() {    struct student a,b,c,*head,*p;    a.num = 99101;    a.score = 89.5;    b.num = 99103;    b.score = 90;    c.num = 99107;    c.score = 85;        /*对结点的num和score成员赋值*/    head = &a;        /*将结点a的起始地址赋给头指针head*/    a.next = &b;        /*将结点b的起始地址赋给a结点的next成员*/    b.next = &c;        /*将结点c的起始地址赋给b结点的next成员*/    c.next = NULL;    /*c结点的next成员不存放其他结点地址*/    p = head;        /*使p指针指向a结点*/    do {        cout<<p->num<<" "<<p->score;    /*输出p指向的结点的数据*/        p=p->next;    /*使p指向下一结点*/    } while (p!=NULL);    /*输出完c结点后p的值为NULL*/    return 0;}