链表:

  1. 由n个节点离散分配。
  2. 每个节点通过指针连接。
  3. 每一个节点由一个前驱节点和一个后驱节点。
  4. 首节点没有前驱节点,尾节点没有后驱节点。

节点由俩个部分组成:

  • 数据域,用来存放数据;
  • 指针域,用来指向下一个节点。

例:构建一个有3个学生的链表,学生信息包括:学号,姓名,成绩。输出链表中每一个学生的全部信息。

#include<stdio.h>
struct Student{
    int num;
    char *name;
    float score;
    struct Student *next;
};
int main(){
    struct Student a,b,c,*head;
    a.num=1;a.name="zhangsan";a.score=90;
    b.num=2;b.name="lisi";b.score=67;
    c.num=3;c.name="wangwu";c.score=87;
    head =&a;a.next=&b;b.next=&c;
    c.next=NULL;
    struct Student *p;p=head;
    while(p!=NULL){
        printf("%d %s %f\n",p->num,p->name,p->score);
        p=p->next;
    }
    return 0;
}

关键字typedef(给变量起外号)

Type define

  1. 简单的用一个新的类型名代替原有类型名
    typedef int apple;
  2. 命名一个新的类型代表结构体变量
    typedef struct Student{ }stu;
  3. 命名一个新的类型代表数组
    typedef int Num[10];
  4. 命名一个新的类型代表指针
    typedef char* String;
End
最后修改:2021 年 04 月 19 日
如果觉得我的文章不错,请随手点赞~