线性表之顺序表实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线性表之顺序表实现相关的知识,希望对你有一定的参考价值。
1 #ifndef __SEQ_LIST_H__ 2 #define __SEQ_LIST_H__ 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <assert.h> 7 8 9 #define SEQLIST_INIT_SIZE 8 //顺序表初始化大小 10 #define INCSIZE 3 //顺序表每次增容大小 11 typedef int ElemType; //顺序表中数据的类型 12 13 typedef struct 14 { 15 ElemType *base; //存储空间基址 16 int capacity; //存储容量 17 int size; //当前长度 18 }SeqList; 19 20 void InitSeqList(SeqList *list); //初始化顺序表 21 void show_list(SeqList *list); //打印链表 22 void push_back(SeqList *list, ElemType x); //尾部插入数据 23 void push_front(SeqList *list, ElemType x); //头部插入数据 24 void insert_pos(SeqList *list, int pos, ElemType x); //按位置插入数据 25 void pop_back(SeqList *list); //尾部删除数据 26 void pop_front(SeqList *list); //头部删除数据 27 void delete_pos(SeqList *list, int pos); //删除指定位置的数据 28 void delete_val(SeqList *list, ElemType x); //删除指定值的数据 29 int find(SeqList *list, ElemType x); //查找数据在顺序表中的下标 30 int length(SeqList *list); //顺序表的长度 31 void sort(SeqList *list); //冒泡排序 32 void reverse(SeqList *list); //顺序表逆置 33 void clear(SeqList *list); //清空顺序表 34 void destroy(SeqList *list); //销毁顺序表 35 36 void merge(SeqList *lt, SeqList *la, SeqList *lb); //合并两个有序顺序表 37 38 #endif //__SEQLIST_H__
1 #include "SeqList.h" 2 3 void InitSeqList(SeqList *list) 4 { 5 list->base = (ElemType *)malloc(SEQLIST_INIT_SIZE * sizeof(ElemType)); 6 assert(list->base != NULL); 7 list->capacity = SEQLIST_INIT_SIZE; 8 list->size = 0; 9 } 10 11 bool Inc(SeqList *list) 12 { 13 ElemType *newbase = (ElemType *)realloc(list->base, sizeof(ElemType)*(list->capacity+INCSIZE)); //新基址 14 if (newbase == NULL) 15 { 16 printf("增配空间失败,内存不足\n"); 17 return false; 18 } 19 20 list->base = newbase; 21 list->capacity += INCSIZE; 22 23 return true; 24 } 25 26 void show_list(SeqList *list) 27 { 28 for (int i = 0; i < list->size; ++i) 29 { 30 printf("%d ", list->base[i]); 31 } 32 printf("\n"); 33 } 34 35 void push_back(SeqList *list, ElemType x) 36 { 37 if (list->size >= list->capacity && !Inc(list)) 38 { 39 printf("顺序表空间已满,尾部插入数据失败\n"); 40 return; 41 } 42 list->base[list->size] = x; 43 ++list->size; 44 } 45 46 void push_front(SeqList *list, ElemType x) 47 { 48 if (list->size >= list->capacity && !Inc(list)) 49 { 50 printf("顺序表空间已满,头部插入数据失败\n"); 51 return; 52 } 53 54 for (int i = list->size; i > 0; --i) 55 { 56 list->base[i] = list->base[i - 1]; 57 } 58 59 list->base[0] = x; 60 ++list->size; 61 } 62 63 void insert_pos(SeqList *list, int pos, ElemType x) 64 { 65 if (list->size >= list->capacity && Inc(list)) 66 { 67 printf("顺序表已满,插入数据失败\n"); 68 return; 69 } 70 71 if (pos<0 || pos>list->size) 72 { 73 printf("插入数据的位置非法,插入数据失败\n"); 74 } 75 76 for (int i = list->size; i > pos; --i) 77 { 78 list->base[i] = list->base[i-1]; 79 } 80 81 list->base[pos] = x; 82 ++list->size; 83 } 84 85 void pop_back(SeqList *list) 86 { 87 if (list->size == 0) 88 { 89 printf("顺序表为空,尾部删除失败\n"); 90 return; 91 } 92 93 --list->size; 94 } 95 96 void pop_front(SeqList *list) 97 { 98 if (list->size == 0) 99 { 100 printf("顺序表为空,头部删除失败\n"); 101 return; 102 } 103 104 for (int i = 0; i < list->size - 1; ++i) 105 { 106 list->base[i] = list->base[i+1]; 107 } 108 109 --list->size; 110 } 111 112 void delete_pos(SeqList *list, int pos) 113 { 114 if (pos<0 || pos>=list->size) 115 { 116 printf("删除位置非法\n"); 117 return; 118 } 119 120 for (int i = pos; i < list->size-1; ++i) 121 { 122 list->base[i] = list->base[i+1]; 123 } 124 125 --list->size; 126 } 127 128 void delete_val(SeqList *list, ElemType x) 129 { 130 int pos = find(list, x); 131 if (pos == -1) 132 { 133 printf("要删除的值在顺序表中不存在"); 134 return; 135 } 136 137 delete_pos(list, pos); 138 } 139 140 int find(SeqList *list, ElemType x) 141 { 142 for (int i = 0; i < list->size; ++i) 143 { 144 if (list->base[i] == x) 145 { 146 return i; 147 } 148 } 149 150 return -1; 151 } 152 153 int length(SeqList *list) 154 { 155 return list->size; 156 } 157 158 void sort(SeqList *list) 159 { 160 for (int i = 0; i < list->size-1; ++i) 161 { 162 for (int j = 0; j < list->size-i-1; ++j) 163 { 164 if (list->base[j] > list->base[j+1]) 165 { 166 ElemType tmp = list->base[j]; 167 list->base[j] = list->base[j+1]; 168 list->base[j+1] = tmp; 169 } 170 } 171 } 172 } 173 174 void reverse(SeqList *list) 175 { 176 if (list->size <= 1) 177 return; 178 179 int low = 0; 180 int high = list->size-1; 181 ElemType tmp; 182 while (low < high) 183 { 184 tmp = list->base[low]; 185 list->base[low] = list->base[high]; 186 list->base[high] = tmp; 187 188 ++low; 189 --high; 190 } 191 } 192 193 void clear(SeqList *list) 194 { 195 list->size = 0; 196 } 197 198 void destroy(SeqList *list) 199 { 200 free(list->base); 201 list->base = NULL; 202 list->capacity = 0; 203 list->size = 0; 204 } 205 206 void merge(SeqList *lt, SeqList *la, SeqList *lb) 207 { 208 lt->capacity = la->size+lb->size; 209 lt->base = (ElemType *)malloc(sizeof(ElemType)*lt->capacity); 210 assert(lt->base != NULL); 211 212 int ia = 0; 213 int ib = 0; 214 int ic = 0; 215 while (ia < la->size && ib < lb->size) 216 { 217 if (la->base[ia] < lb->base[ib]) 218 lt->base[ic++] = la->base[ia++]; 219 else 220 lt->base[ic++] = lb->base[ib++]; 221 } 222 223 while(ia < la->size) 224 { 225 lt->base[ic++] = la->base[ia++]; 226 } 227 while (ib < lb->size) 228 { 229 lt->base[ic++] = lb->base[ib++]; 230 } 231 232 lt->size = la->size+lb->size; 233 }
以上是关于线性表之顺序表实现的主要内容,如果未能解决你的问题,请参考以下文章