当前位置:首页 > 通信资讯 > 正文
目录
  • 创建一个结构体用于存放顺序表相关数据
  • 初始化顺序表
  • 插入元素
    • 先检查容量是否够用
  • 删除元素
    • 元素修改
      • 查找元素
        • 排序元素
          • 元素反转
            • 源码
              • SeqList.c
              • test.c
              • SeqList.h
            • 总结

              创建一个结构体用于存放顺序表相关数据

              ?
              1 2 3 4 5 6 7 #define SEQTYPE int typedef struct SeqList { SEQTYPE* data; int size; //有效数据个数 int capacity; //容量 }SeqList;

              初始化顺序表

              ?
              1 2 3 4 5 6 7 8 void SeqListInit(SeqList* pq) { CheckNull(pq); pq->data = NULL; pq->capacity = 0; pq->size = 0; }

              插入元素

              1. 插入到表头;
              2. 插入到指定位置;
              3. 插入到尾部;

              先检查容量是否够用

              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 void CheckCapacity(SeqList* pq) { CheckNull(pq); //如果空间满了,扩容 if (pq->size >= pq->capacity) { int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2; SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity); if (new == NULL) { perror("realloc"); exit(-1); } pq->data = new; pq->capacity = newcapacity; } puts("增容成功"); } //往顺序表指定位置插入数据 void SeqListInsert(SeqList* pq, int pos) { CheckNull(pq); assert(pos <= pq->size); SEQTYPE InsertVal; if (pos == -1) { printf("请分别输入添加的数据和位置,空格隔开:>"); scanf("%d %d", &InsertVal, &pos); if (pos > pq->size) { printf("请正确输入\n"); return; } } else { printf("请输入添加的数据:>"); scanf("%d", &InsertVal); } //检查容量是否足够 CheckCapacity(pq); //插入数据 int end = pq->size; int begin = pos; while (begin < end) { pq->data[end] = pq->data[end - 1]; --end; } pq->data[pos] = InsertVal; ++pq->size; printf("添加成功\n"); } //往顺序表末位置插入数据 void SeqListPushBack(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, pq->size); } //往顺序表首位置插入数据 void SeqListPushFront(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, 0); }

              删除元素

              1. 删除首元素;
              2. 删除指定位置元素;
              3. 删除尾部元素;
              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 //从顺序表指定位置删除数据 void SeqListErase(SeqList* pq, int pos) { CheckNull(pq); if (pos == -1) { printf("请输入要删除数据的位置:>"); scanf("%d", &pos); if (pos < 0 || pos >= pq->size) { printf("请正确输入\n"); return; } } int begin = pos; int end = pq->size - 1; while (begin < end) { pq->data[begin] = pq->data[begin + 1]; ++begin; } --pq->size; puts("删除成功"); } //从顺序表末位置删除数据 void SeqListPophBack(SeqList* pq) { CheckNull(pq); SeqListErase(pq, pq->size - 1); } //从顺序表首位置删除数据 void SeqListPophFront(SeqList* pq) { CheckNull(pq); SeqListErase(pq, 0); }

              元素修改

              1. 找到目标元素;
              2. 直接修改该元素的值;
              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 //修改顺序表指定位置数据 void SeqListModify(SeqList* pq) { CheckNull(pq); int pos; SEQTYPE x; printf("请输入修改的位置和新的数据,空格隔开:>"); scanf("%d %d", &pos, &x); if (pos < 0 && pos >= pq->size) { printf("请正确输入\n"); return; } pq->data[pos] = x; puts("修改成功"); }

              查找元素

              查找目标元素,算法多种,比如二分,插值等等,这里使用顺序查找算法,具体代码如下:

              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 //查找所需数据是否存在顺序表中 void SeqListFindData(SeqList* pq) { CheckNull(pq); SEQTYPE x; printf("请输入要查找的数据:>"); scanf("%d", &x); for (int i = 0; i < pq->size; i++) { if (pq->data[i] == x) { printf("所需查询数据存在,下标为:>%d\n", i); return; } } printf("找不到\n"); }

              排序元素

              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 //排序顺序表 void SeqListSort(SeqList* pq) { CheckNull(pq); int option = 0; printf("输入0为升序,1为降序:>"); scanf("%d", &option); for (int i = 0; i < pq->size - 1; i++) { for (int j = 0; j < pq->size - i - 1; j++) { if (pq->data[j] > pq->data[j + 1]) { SEQTYPE tmp = pq->data[j]; pq->data[j] = pq->data[j + 1]; pq->data[j + 1] = tmp; } } } if (option) { SeqListReverse(pq); return; } }

              元素反转

              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 //顺序表反转 void SeqListReverse(SeqList* pq) { CheckNull(pq); int left = 0; int right = pq->size - 1; while (left < right) { SEQTYPE tmp = pq->data[left]; pq->data[left] = pq->data[right]; pq->data[right] = tmp; ++left; --right; } }

              源码

              • 以上是顺序表常用的功能操作,下面附上完整代码,VS2019环境

              SeqList.c

              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 #include "SeqList.h" void CheckNull(SeqList* pq) { if (pq == NULL) { perror("pq::"); exit(-1); } } //初始化顺序表 void SeqListInit(SeqList* pq) { CheckNull(pq); pq->data = NULL; pq->capacity = 0; pq->size = 0; } void SeqListDestory(SeqList* pq) { CheckNull(pq); free(pq->data); pq->data = NULL; pq->size = 0; pq->capacity = 0; } void CheckCapacity(SeqList* pq) { CheckNull(pq); //如果空间满了,扩容 if (pq->size >= pq->capacity) { int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2; SEQTYPE* new = (SEQTYPE*)realloc(pq->data, sizeof(SEQTYPE) * newcapacity); if (new == NULL) { perror("realloc"); exit(-1); } pq->data = new; pq->capacity = newcapacity; } puts("增容成功"); } void SeqListPrint(SeqList* pq) { CheckNull(pq); if (pq->size == 0) printf("\n"); else { for (int i = 0; i < pq->size; i++) { printf("%d ", pq->data[i]); } puts("\n--------------------------------------"); } } //往顺序表末位置插入数据 void SeqListPushBack(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, pq->size); } //往顺序表首位置插入数据 void SeqListPushFront(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, 0); } //往顺序表指定位置插入数据 void SeqListInsert(SeqList* pq, int pos) { CheckNull(pq); assert(pos <= pq->size); SEQTYPE InsertVal; if (pos == -1) { printf("请分别输入添加的数据和位置,空格隔开:>"); scanf("%d %d", &InsertVal, &pos); if (pos > pq->size) { printf("请正确输入\n"); return; } } else { printf("请输入添加的数据:>"); scanf("%d", &InsertVal); } //检查容量是否足够 CheckCapacity(pq); //插入数据 int end = pq->size; int begin = pos; while (begin < end) { pq->data[end] = pq->data[end - 1]; --end; } pq->data[pos] = InsertVal; ++pq->size; printf("添加成功\n"); } //从顺序表指定位置删除数据 void SeqListErase(SeqList* pq, int pos) { CheckNull(pq); if (pos == -1) { printf("请输入要删除数据的位置:>"); scanf("%d", &pos); if (pos < 0 || pos >= pq->size) { printf("请正确输入\n"); return; } } int begin = pos; int end = pq->size - 1; while (begin < end) { pq->data[begin] = pq->data[begin + 1]; ++begin; } --pq->size; puts("删除成功"); } //从顺序表末位置删除数据 void SeqListPophBack(SeqList* pq) { CheckNull(pq); SeqListErase(pq, pq->size - 1); } //从顺序表首位置删除数据 void SeqListPophFront(SeqList* pq) { CheckNull(pq); SeqListErase(pq, 0); } //修改顺序表指定位置数据 void SeqListModify(SeqList* pq) { CheckNull(pq); int pos; SEQTYPE x; printf("请输入修改的位置和新的数据,空格隔开:>"); scanf("%d %d", &pos, &x); if (pos < 0 && pos >= pq->size) { printf("请正确输入\n"); return; } pq->data[pos] = x; puts("修改成功"); } //查找顺序表指定位置数据 void SeqListFindPos(SeqList* pq) { CheckNull(pq); int pos; printf("请输入要查找数据的位置:>"); scanf("%d", &pos); if (pos < 0 && pos >= pq->size) { printf("请正确输入\n"); return; } for (int i = 0; i < pq->size; i++) { if (pq->data[i] == pq->data[pos]) { printf("查找位置的数据为:>%d\n", pq->data[pos]); break; } } } //查找所需数据是否存在顺序表中 void SeqListFindData(SeqList* pq) { CheckNull(pq); SEQTYPE x; printf("请输入要查找的数据:>"); scanf("%d", &x); for (int i = 0; i < pq->size; i++) { if (pq->data[i] == x) { printf("所需查询数据存在,下标为:>%d\n", i); return; } } printf("找不到\n"); } //排序顺序表 void SeqListSort(SeqList* pq) { CheckNull(pq); int option = 0; printf("输入0为升序,1为降序:>"); scanf("%d", &option); for (int i = 0; i < pq->size - 1; i++) { for (int j = 0; j < pq->size - i - 1; j++) { if (pq->data[j] > pq->data[j + 1]) { SEQTYPE tmp = pq->data[j]; pq->data[j] = pq->data[j + 1]; pq->data[j + 1] = tmp; } } } if (option) { SeqListReverse(pq); return; } } //顺序表反转 void SeqListReverse(SeqList* pq) { CheckNull(pq); int left = 0; int right = pq->size - 1; while (left < right) { SEQTYPE tmp = pq->data[left]; pq->data[left] = pq->data[right]; pq->data[right] = tmp; ++left; --right; } }

              test.c

              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 #include "SeqList.h" void menu() { printf("######################################################\n"); printf("##### 1. Print 2. Insert #####\n"); printf("##### 3. PushFront 4. PushBack #####\n"); printf("##### 5. PopFront 6. PopBack #####\n"); printf("##### 7. FindPos 8. FindData #####\n"); printf("##### 9. Modify 10. Erase #####\n"); printf("##### 11. EMPTY 12. Sort #####\n"); printf("##### 13. Reverse 0. Exit #####\n"); printf("######################################################\n"); } enum Option { EXIT, PRINT, INSERT, PUSHFRONT, PUSHBACK, POPFRONT, POPBACK, FINDPOS, FINDDATA, MODIFY, ERASE, EMPTY, SORT, REVERSE, }; int main() { int option = 0; int posi = -1; SeqList s; SeqListInit(&s); do { menu(); printf("请选择:>"); scanf("%d", &option); switch (option) { case PRINT: SeqListPrint(&s); break; case INSERT: SeqListInsert(&s, posi); break; case PUSHFRONT: SeqListPushFront(&s); break; case PUSHBACK: SeqListPushBack(&s); break; case POPFRONT: SeqListPophFront(&s); break; case POPBACK: SeqListPophBack(&s); break; case FINDPOS: SeqListFindPos(&s); break; case FINDDATA: SeqListFindData(&s); break; case MODIFY: SeqListModify(&s); break; case ERASE: SeqListErase(&s, posi); break; case EMPTY: SeqListDestory(&s); break; case SORT: SeqListSort(&s); break; case REVERSE: SeqListReverse(&s); break; case EXIT: SeqListDestory(&s); printf("退出\n"); break; default: printf("请正确输入\n"); break; } } while (option); return 0; }

              SeqList.h

              ?
              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <errno.h> #include <string.h> // #define SEQTYPE int typedef struct SeqList { SEQTYPE* data; int size; //有效数据个数 int capacity; //容量 }SeqList; //初始化顺序表 void SeqListInit(SeqList* pq); //销毁顺序表 void SeqListDestory(SeqList* pq); //打印顺序表 void SeqListPrint(SeqList* pq); //往顺序表指定位置插入数据 void SeqListInsert(SeqList* pq, int pos); //往顺序表末位置插入数据 void SeqListPushBack(SeqList* pq); //往顺序表首位置插入数据 void SeqListPushFront(SeqList* pq); //从顺序表指定位置删除数据 void SeqListErase(SeqList* pq, int pos); //从顺序表末位置删除数据 void SeqListPophBack(SeqList* pq); //从顺序表首位置删除数据 void SeqListPophFront(SeqList* pq); //修改顺序表指定位置数据 void SeqListModify(SeqList* pq); //查找顺序表指定位置数据 void SeqListFindPos(SeqList* pq); //查找所需数据是否存在顺序表中 void SeqListFindData(SeqList* pq); //排序顺序表 void SeqListSort(SeqList* pq); //顺序表反转 void SeqListReverse(SeqList* pq);

              总结

              到此这篇关于C语言实现顺序表的基本操作指南的文章就介绍到这了,更多相关C语言实现顺序表基本操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

              原文链接:https://blog.csdn.net/weixin_45266788/article/details/120694962

              如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

              为您推荐:

              发表评论

              ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。