本文采用项目来实现顺序表,分为SeqList.h、SeqList.c 以及exercise.c
SeqList.h
//SeqList.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int Type;
typedef struct Seqlist
{
Type* p;//指向顺序表内存空间的指针
int size;//顺序表现有数据数量
int capacity;//顺序表总容量
}SL;
void SLInit(SL* ps);
void SLCheckCapacity(SL* ps);
void SLPrint(SL* ps);
void SLInsert(SL* ps, int pos, Type x);
void SLdelete(SL* ps, int pos);
void SLFind(SL* ps, Type x);
void SLModify(SL* ps, int pos, Type x);
void SLGetline(SL* ps);
void SLdestroy(SL* ps);
SeqList.c
//SeqList.c
#include"SeqList.h"
void SLInit(SL * ps)//初始化
{
ps->capacity = 2;//初始容量为2
ps->size = 0;
ps->p = (Type*)malloc(2*sizeof(Type));//初始容量为2
}
void SLCheckCapacity(SL* ps)//检查是否容量是否满了,满了则扩容
{
if (ps->capacity == ps->size)
{
Type* newcapacity = (Type*)realloc(ps->p, ps->capacity*2*sizeof(Type));//二倍扩容
ps->p = newcapacity;//新容量
ps->capacity = ps->capacity * 2;
}
}
void SLPrint(SL* ps)//打印顺序表
{
printf("顺序表如下:\n");
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->p[i]);
}
printf("\n");
}
void SLInsert(SL* ps, int pos, Type x)//插入数据
{
SLCheckCapacity(ps); //检查是否满容
assert(pos <= ps->capacity);//断言要插入的位置是否超过上限
int end=ps->size;
while (end > pos)
{
ps->p[end] = ps->p[end - 1];//从末尾数据开始向后移位
end--;
}
ps->p[pos] = x;//插入数据到对应位置
ps->size++;
}
void SLdelete(SL* ps, int pos)//删除数据
{
SLCheckCapacity(ps);//检查是否满容
assert(pos < ps->capacity);//断言要删除的位置是否超过上限
int start = pos;
while (start < ps->size - 1)
{
ps->p[start] = ps->p[start + 1];//从start开始数据向前移位
start++;
}
ps->size--;
}
void SLFind(SL* ps, Type x)//查找数据
{
printf("该数据的位置为:");
int judge = 0;
for (int i = 0; i < ps->size; i++)
{
if (ps->p[i] == x)
{
printf("%d ", i);
judge = 1;
}
}
if (judge == 0)
printf("未找到该数值\n");
else
printf("\n");
}
void SLModify(SL* ps, int pos, Type x)//修改数据
{
assert(pos < ps->size);//断言要修改的位置是否超过上限
ps->p[pos] = x;
}
void SLdestroy(SL*ps)//销毁顺序表
{
ps->capacity = 0;
ps->size = 0;
free(ps->p);
ps->p=NULL;
}
void SLGetline(SL* ps)//整行插入数据
{
Type a;
while (scanf("%d", &a) ==1)
{
SLInsert(ps, ps->size, a);
}
}
exercise.c
//exercise.c
#include"SeqList.h"
void menu()
{
printf("*******************动态顺序表*******************\n");
printf("************1.插入一行数据,以#结束**************\n");
printf("************2.头插数据*****3.尾插数据***********\n");
printf("************4.头删数据*****5.尾删数据***********\n");
printf("************6.任意插入*****7.任意删除***********\n");
printf("************8.修改数据*****9.查找数据***********\n");
printf("************10.打印数据****0.退出程序***********\n");
printf("请输入操作序号:");
}
int main()
{
SL a;
SL* p = &a;
int pos,n=0;
SLInit(p);
Type x;
do
{
menu();
scanf("%d", &n);
switch (n)
{
case 1: {printf("请输入要插入的数据(以#结束):"); SLGetline(p); getchar(); }break;
case 2: {printf("请输入要插入的数据:"); scanf("%d", &x); SLInsert(p, 0, x); }break;
case 3: {printf("请输入要插入的数据:"); scanf("%d", &x); SLInsert(p, p->size, x); }break;
case 4: {SLdelete(p, 0); printf("头删完成\n"); }break;
case 5: {SLdelete(p, p->size); printf("尾删完成\n"); }break;
case 6: {printf("请输入要插入的数据:"); scanf("%d", &x); printf("请输入要插入的位置:"); scanf("%d", &pos); SLInsert(p, pos, x); }break;
case 7: {printf("请输入要删除的位置:"); scanf("%d", &pos); SLdelete(p, pos); }break;
case 8: {printf("请输入要修改的位置:"); scanf("%d", &pos); printf("请输入修改后的数据:"); scanf("%d", &x); SLModify(p, pos, x); }break;
case 9: {printf("请输入要查找的数据:"); scanf("%d", &x); SLFind(p, x); }break;
case 10: {SLPrint(p); }break;
default:break;
}
printf("\n");
} while (n);
printf("正在删除顺序表...\n");
SLdestroy(p);
printf("删除成功");
return 0;
}
运行样例