经过三天的时间终于把顺序表的操作实现搞定了。(主要是在测试部分停留了太长时间)
1;线性表顺序存储的概念:指的是在内存中用一段地址连续的存储单元依次存储线性表中的元素。
2;采用的实现方式:一段地址连续的存储单元可以用固定数组或者动态存储结构来实现,这里采用动态分配存储结构。
3;顺序表的定义及操作集合:头文件为defs.h
1 #ifndef _DEFS_H 2 #define _DEFS_H 3 4 #include<stdio.h> 5 #include<stdlib.h> 6 #include<malloc.h> 7 8 #define LIST_INIT_MAX 10 //长表为10 9 #define LIST_INCREMENT 2 //短表为2 10 typedef struct 11 { 12 int * elem; //采用动态存储分配结构 13 int length; 14 int listsize; 15 }sqlist; 16 //线性表操作集合 17 void InitList(sqlist *L); //初始化,动态分配一块存储空间 18 void DestroyList(sqlist *L); //释放这一段存储空间(撤销对应于动态) 19 void ClearList(sqlist *L); 20 void ListEmpty(sqlist L); 21 int ListLength(sqlist L); 22 int GetElem(sqlist L, int i, int *e); 23 void LocateList(sqlist L, int e); //在表中查找值为e的元素 24 int PriorElem(sqlist L, int cur_e, int *pri_e); //求当前元素的前驱 25 int NextElem(sqlist L, int cur_e, int *Nex_e); //求当前元素的后继 26 int ListInsert(sqlist &L, int i, int e); //插入操作 27 int ListDelete(sqlist &L, int i, int *e); //删除操作 28 void TravelList(sqlist L); //便历操作 29 #endif
4;顺序表结构体示意图
5;InitList函数实现
1 #include"defs.h" 2 3 void InitList(sqlist *L) 4 { 5 L->elem = (int *)malloc(LIST_INIT_MAX*sizeof(int)); //初始化指针 6 if (!L->elem) 7 exit(-1); 8 L->length = 0; //初始化当前元素个数 9 L->listsize = LIST_INIT_MAX; //初始化表长 10 }