每次随机插入都要判断容量是否足够

随机删除要判断i是否再数组下标范围内，采用断言，方便找错

//头文件 #pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h>
//静态版本 //typedef int DataType; //#define N 1000 //typedef struct SeqList //{ //
DataType a[N]; // int sz; //}SL; //动态版本 typedef int DataType; typedef struct
SeqList { DataType* a; int sz;//有效数据的个数 int capacity;//以开辟空间的大小 }SL; //初始化顺序表
void SeqListInit(SL* ps); //检查容量 void SeqListCheckCapacity(SL* ps); //销毁循序表
void SeqListDestroy(SL* ps); //打印顺序表 void SeqListPrint(SL* ps); //尾插 void
SeqListPushBack(SL* ps, DataType x); //尾删 void SeqListPopBack(SL* ps); //头插
void SeqListPushFront(SL* ps, DataType x); //头删 void SeqListPopFront(SL* ps);
//查找 int SeqListFind(SL* ps, DataType pos); //随机位置插入 void SeqListInsert(SL* ps,
DataType x); //随机位置删除 void SeqListDelete(SL* ps); //定义文件 #include"SeqList.h"
void SeqListInit(SL* ps) { ps->a = NULL; ps->sz = 0; ps->capacity = 0; } void
SeqListCheckCapacity(SL* ps) { if (ps->sz == ps->capacity) { int newcapacity =
ps->capacity == 0 ? 4 : 2 * ps->capacity; DataType* tmp =
(DataType*)realloc(ps->a, sizeof(DataType) * newcapacity); if (tmp == NULL) {
printf("增容失败\n"); exit(-1); } ps->capacity = newcapacity; ps->a = tmp; } } void
SeqListDestroy(SL* ps) { free(ps->a); ps->a = NULL; ps->sz = 0; ps->capacity =
0; } void SeqListPrint(SL* ps) { int i = 0; for (i = 0; i < ps->sz; i++) {
printf("%d ", ps->a[i]); } printf("\n"); } void SeqListPushBack(SL* ps,
DataType x) { SeqListCheckCapacity(ps); ps->a[ps->sz] = x; ps->sz++; } void
SeqListPopBack(SL* ps) { assert(ps->sz > 0); ps->sz--; } void
SeqListPushFront(SL* ps,DataType x) { SeqListCheckCapacity(ps); int end1 =
ps->sz; while (end1) { int end2 = end1 - 1; ps->a[end1--] = ps->a[end2]; }
ps->a[end1] = x; ps->sz++; } void SeqListPopFront(SL* ps) { assert(ps->sz > 0);
int begin1 = 0; while (begin1 < ps->sz) { int begin2 = begin1 + 1;
ps->a[begin1++] = ps->a[begin2]; } ps->sz--; } int SeqListFind(SL* ps, DataType
pos) { int cur = 0; while (cur < ps->sz) { if (ps->a[cur] == pos) { return cur;
} else { cur++; } } return -1; } void SeqListInsert(SL* ps, DataType x) {
SeqListCheckCapacity(ps); int i = SeqListFind(ps, 5); assert(i >= 0 && i <=
ps->sz); int end1 = ps->sz; while (end1 > i) { int end2 = end1 - 1;
ps->a[end1--] = ps->a[end2]; } ps->a[end1] = x; ps->sz++; } void
SeqListDelete(SL* ps) { int i = SeqListFind(ps, 5); assert(i >= 0 && i <=
ps->sz); while (i < ps->sz) { int cur = i + 1; ps->a[i++] = ps->a[cur]; }
ps->sz--; } //测试文件 #include"SeqList.h" void test1() { SL s; SeqListInit(&s);
SeqListPushBack(&s, 1); SeqListPushBack(&s, 2); SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4); SeqListPushBack(&s, 5); SeqListPushBack(&s, 6);
SeqListPushBack(&s, 7); SeqListPrint(&s); SeqListPopBack(&s);
SeqListPopBack(&s); SeqListPopBack(&s); SeqListPopBack(&s); SeqListPopBack(&s);
SeqListPrint(&s); SeqListDestroy(&s); } void test2() { SL s; SeqListInit(&s);
SeqListPushFront(&s, 1); SeqListPushFront(&s, 2); SeqListPushFront(&s, 3);
SeqListPushFront(&s, 4); SeqListPushFront(&s, 5); SeqListPushFront(&s, 6);
SeqListPushFront(&s, 7); SeqListPrint(&s); SeqListPopFront(&s);
SeqListPopFront(&s); SeqListPopFront(&s); SeqListPopFront(&s);
SeqListPopFront(&s); SeqListPrint(&s); /*int ret = SeqListFind(&s, 3);
printf("查找的数的下标为：%d\n", ret);*/ SeqListDestroy(&s); } void test3() { SL s;
SeqListInit(&s); SeqListPushBack(&s, 1); SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SeqListPushBack(&s, 5);
SeqListPushBack(&s, 6); SeqListPushBack(&s, 7); SeqListInsert(&s, 15);
SeqListPrint(&s); SeqListDelete(&s); SeqListPrint(&s); } int main() {
//test1(); //test2(); test3(); return 0; }

GitHub

Gitee