1.链表:用于保存可变数量的数据,
可用于保存很多不同类型的数据,
常用指针来访问链表的下一结构
2.链表用法:
typedef struct island { char *names; char *opens; char *closes; struct island *next; }island;
注意:当用typedef命令定义结构时可以跳过为结构起名这步,但在递归结构中,需要包含一个相同类型的指针,C语言的语法不允许用typedef别名来声明它,因此必须为结构起一个名字,这就是为什么这里的结构叫struct island
3.连接链表的各个部分:
island amity = {"Amity","09:00","17:00",NULL}; island craggy = {"Craggy","09:00","17:00",NULL}; amity.next = &craggy;
4.插入一个新的部分
isla_nublar.next = &skull;
skull.net = &shutter;
5.一个很长的链表,使用其中的第700个元素,必须从第一个开始一直读下去。
6.快速插入数据:链表
直接访问元素:数组
练习:
#include<stdio.h> typedef struct island { char *name; char *opens; char *closes; struct island *next; }island; void display(island *start) { island *i = start; for(;i != NULL;i = i->next)//注意这一段的格式 printf("Name:%s open:%s-%s ",i->name,i->opens,i->closes); } int main() { island amity = {"Amity","09:00","17:00",NULL}; island craggy = {"Craggy","09:00","17:00",NULL}; island isla_nublar = {"Isla Nublar","09:00","17:00",NULL}; island shutter = {"Shutter","09:00","17:00",NULL}; island skull = {"Skull","09:00","17:00",NULL}; amity.next = &craggy; craggy.next = &isla_nublar; isla_nublar.next = &skull; skull.next = &shutter; display(&amity); return 0; }