静态链表(static linked list):用数组来描述的链表,用数组元素的下标来模拟单链表的指针。这种描述方法叫做游标实现法。
游标 | 5 | 2 | 3 | 4 | 5 | 6 | 7 | … | 1 |
---|---|---|---|---|---|---|---|---|---|
数据 | A | B | D | E | … | ||||
下标 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | … | 20 |
线性表的静态链表存储结构如下:
#define MAXSIZE 20
#define ElemType int
typedef struct{
ElemType data; //数据
int cur; //游标(cursor)
} StaticLinkList[MAXSIZE];
静态链表初始化:
对静态链表的初始化相当于初始化数组。
#include <stdio.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 20
#define ElemType int
typedef int status;
typedef struct{
ElemType data; //数据
int cur; //游标cursor
} StaticLinkList[MAXSIZE];
/* 初始化静态链表 */
status initList(StaticLinkList space){
int i;
for(i = 0; i < MAXSIZE; i++){
space[i].data = 0; //初始化数据为0
space[i].cur = i + 1;
}
//初始化时将最后一个游标指向0,实际有数据时指向第一个有数值的元素的下标
space[MAXSIZE - 1].cur = 0;
return OK;
}
/* 输出静态链表 */
status printList(StaticLinkList space){
int i;
for(i = 0; i < MAXSIZE; i++){
printf("space[%d].data = %c; space[%d].cur = %d\n",i,space[i].data,i,space[i].cur);
}
return OK;
}
int main(){
//初始化静态链表
Stat