线性表的顺序存储结构

线性表的顺序存储结构:

1 /*
2 ** 线性表的顺序存储结构 */
3 #define SQLIST_INIT_SIZE 100 //顺序表存储空间初始分配量
4 #define SQLIST_INCREMENT 10  //存储空间分配增量
5 typedef struct tagSqList {
6     ListElemType *data;//线性表存储数据基地址
7              int  length;//线性表长度(当前元素个数)
8              int  size;//为线性表分配的存储空间大小(以sizeof(ListElemType)为单位)
9 }SqList;

线性表的基本操作:

  1 #include "sqlist_algo.h"
  2 #include <stdlib.h>
  3 #include <memory.h>
  4 
  5 /*
  6 ** 构造一个空的线性顺序表,并将长度设置为0
  7 */
  8 void SqList_Init(SqList *list)
  9 {
 10     list->data = (ListElemType *)malloc(SQLIST_INIT_SIZE*sizeof(ListElemType));
 11     if (!list->data) exit(EXIT_FAILURE);//存储空间分配失败
 12     list->length = 0;//线性表长度(当前元素个数)设置为0
 13     list->size = SQLIST_INIT_SIZE;//为线性表分配的存储空间大小
 14 }
 15 
 16 /*
 17 ** 插入
 18 */
 19 int SqList_Insert(SqList *list, int index, ListElemType elem)
 20 {
 21     ListElemType *base, *pMove, *pInsert;
 22     if (index < 1 || index > list->length + 1) return 0;//参数出错
 23     if (list->length == list->size){//当前存储空间已满
 24         base = (ListElemType *)realloc(list->data, 
 25             (list->size + SQLIST_INCREMENT) * sizeof(ListElemType));
 26         if (!base) exit(EXIT_FAILURE);
 27         list->data = base;//将新申请的内存空间首地址赋给data
 28         list->size += SQLIST_INCREMENT;//增加存储空间容量
 29     }
 30     pInsert = list->data + index - 1;//pInsert为插入位置,数组下标
 31     for (pMove = list->data + list->length - 1; pMove >= pInsert; --pMove){
 32         *(pMove+1) = *pMove;//插入位置及之后的元素依次后移(从表尾开始)
 33     }
 34     *pInsert = elem;//插入元素
 35     ++list->length;//表长加1
 36     return 1;
 37 }
 38 
 39 /*
 40 ** 判断是否为空
 41 */
 42 int SqList_Empty(SqList list)
 43 {
 44     if (list.length == 0) return 1;
 45     return 0;
 46 }
 47 
 48 /*
 49 ** 获取表长
 50 */
 51 int SqList_Length(SqList list)
 52 {
 53     return list.length;
 54 }
 55 
 56 /*
 57 ** 遍历线性表
 58 */
 59 void SqList_Traverse(SqList list, void (*visit)(ListElemType))
 60 {
 61     ListElemType *p = list.data;//p指向第一个元素
 62     int i;
 63     for (i = 1; i <= list.length; i++){
 64         visit(*p++);
 65     }
 66 }
 67 
 68 /*
 69 ** 删除
 70 */
 71 int SqList_Delete(SqList *list, int index, ListElemType *elem)
 72 {
 73     ListElemType *pDel, *pTail;
 74     if (index < 1 || index > list->length) return 0;//参数非法
 75     pDel = list->data + index - 1;//pDel指向待删除元素位置
 76     *elem = *pDel;//被删除元素值赋给elem
 77     pTail = list->data + list->length - 1;//pTail指向表尾元素位置
 78     for (++pDel; pDel <= pTail; ++pDel){
 79         *(pDel - 1) = *pDel;//被删除元素以后的元素依次向前移动一位
 80     }
 81     list->length--;//表长减1
 82     return 1;
 83 }
 84 
 85 /*
 86 ** 定位,返回与elem满足compare()关系的元素的位置
 87 */
 88 int SqList_Locate(SqList list, ListElemType elem, int (*compare)(ListElemType, ListElemType))
 89 {
 90     int i = 1;//初始值为第一个元素的位序
 91     ListElemType *p = list.data;//p指向第一个元素位置
 92     while (i <= list.length && !compare(elem, *p++)){
 93         ++i;//没找到满足条件的就继续找
 94     }
 95     if (i <= list.length) return i;//找到
 96     return 0;//没有找到
 97 }
 98 
 99 /*
100 ** 清空
101 */
102 void SqList_Clear(SqList *list)
103 {
104     list->length = 0;
105 }
106 
107 /*
108 ** 销毁
109 */
110 void SqList_Destroy(SqList *list)
111 {
112     free(list->data);
113     list->data = NULL;
114     list->length = 0;
115     list->size = 0;
116 }

测试代码如下:

 1 #include <stdio.h>
 2 #include "sqlist_algo.h"
 3 
 4 void visit(ListElemType e)
 5 {
 6     printf(" %d ", e);
 7 }
 8 
 9 int compare(ListElemType e1, ListElemType e2)
10 {
11     if (e1 == e2) return 1;
12     return 0;
13 }
14 
15 //For Test
16 int main(int argc, char **argv)
17 {
18     SqList List;
19     ListElemType e;
20     int i = 0, index;
21     
22     //初始化空表
23     SqList_Init(&List);
24 
25     //插入元素
26     scanf("%d", &e);
27     while (e > 0){
28         SqList_Insert(&List, ++i, e);
29         scanf("%d", &e);
30     }
31     //判断是否为空
32     printf("Empty : %d
", SqList_Empty(List));
33     //获取表长
34     printf("Length : %d
", SqList_Length(List));
35     //遍历
36     SqList_Traverse(List, visit);
37     //删除
38     printf("
Del Index: ");
39     scanf("%d", &index);
40     if (SqList_Delete(&List, index, &e)){
41         printf("Delete %d-th Element %d
", index, e);
42     }
43     SqList_Traverse(List, visit);
44     //定位
45     printf("
Locate : ");
46     scanf("%d", &e);
47     printf("Result: %d
", SqList_Locate(List, e, compare));
48 
49     return 0;
50 }
原文地址:https://www.cnblogs.com/xiaomanon/p/4493826.html