1、代码头文件
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLDataType;
//typedef struct SeqList
//{//静态顺序表
// SLDataType a[1000];
// int size; //数据个数
//}SL;
typedef struct SeqList
{//动态顺序表
SLDataType* a;//存储空间首地址
int size;
int capacity;//数组实际能存储数据的空间容量多大,当前分配的存储容量
}SL;
//接口函数
void SeqListInit(SL* ps);//顺序表初始化
void SeqListDestory(SL* ps);//顺序表销毁
void SeqListCheckCapacity(SL*ps);//检查扩容
void SeqListPushBack(SL* ps, SLDataType x);//尾插
void SeqListPopBack(SL* ps);//尾删
void SeqListPrint(SL* ps);//顺序表打印
void SeqListPushFront(SL* ps,SLDataType x);//顺序表头插
void SeqListPopFront(SL* ps);//头删
int SeqListFind(SL* ps, SLDataType x);//顺序表查找,返回下标
void SeqListInsert(SL* ps, int pos, SLDataType x);//指定pos下标插入
void SeqListErase(SL* ps, int pos);//指定pos下标删除
2、接口函数代码
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
void SeqListInit(SL* ps) {//顺序表初始化
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void SeqListCheckCapacity(SL* ps)//检查扩容
{
if (ps->size == ps->capacity) {
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//tmp临时空间
SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
if (tmp == NULL) {
printf("realloc fail\n");
exit(-1);//退出
}
ps->a = tmp;
ps->capacity = newcapacity;
}
}
void SeqListPushBack(SL* ps, SLDataType x) {//尾插
//ps->a[ps->size] = x;//内存足够直接插
//ps->size++;
//不够就扩容
SeqListCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
//SeqListInsert(ps,ps->size,x)//SeqListInsert的复用
}
void SeqlistDestory(SL* ps)//顺序表销毁
{
free(ps->a);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void SeqListPrint(SL* ps)//顺序表打印
{
for (int i = 0; i < ps->size; ++i)
{
printf("%d ", ps->a[i]);
}
printf("\n");//打印完换行
}
void SeqListPopBack(SL* ps)//尾删
{
//ps->a[ps->size - 1] = 0;//可有可无,size标识着存着的多少有效数据
//ps->size--;
/*if (ps->size > 0)
{
ps->size--;
}*/
//或者
assert(ps->size > 0);
ps->size--;
//SeqListErase(ps, ps->size - 1);//函数复用
}
void SeqListPushFront(SL* ps,SLDataType x)//顺序表头插
{
SeqListCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= 0) {
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
//SeqListInsert(ps,0,x)
}
void SeqListPopFront(SL* ps) //头删
{
assert(ps->size > 0);
int begin = 1;
while (begin < ps->size) {
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
//SeqListErase(ps, 0);
}
int SeqListFind(SL* ps, SLDataType x)//顺序表查找,返回下标
{
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListInsert(SL* ps, int pos, SLDataType x)//指定pos下标插入
{
//if (pos > ps->size || pos < 0) {//温柔的处理方式
// printf("pos error\n");
// return;
//}
assert(pos <= ps->size && pos >= 0);//断言
//先挪动数据再插入
SeqListCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
void SeqListErase(SL* ps, int pos)//指定pos下标删除
{
assert(pos >= 0 && pos < ps->size);
int begin = pos+1;//将删除元素后面的依次往前挪
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}
3、接口函数的实现
下面是按顺序调用上面的函数代码(可复制到电脑上运行查看)
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
void TestSeqList1() {
SL s1;
SeqListInit(&s1);
SeqListPushBack(&s1, 1);//尾插
SeqListPushBack(&s1, 2);
SeqListPushBack(&s1, 3);
SeqListPushBack(&s1, 4);
SeqListPrint(&s1);//顺序表打印
SeqListPopBack(&s1);
SeqListPopBack(&s1);
SeqListPopBack(&s1);
SeqListPopBack(&s1);
//SeqListPopBack(&s1);//size=-1了
SeqListPushBack(&s1, 1);//把1吞了,前面多删了一个
SeqListPushBack(&s1, 2);
SeqListPrint(&s1);
//SeqListDestory(&s1);
}
void TestSeqList2() {
SL s1;
SeqListInit(&s1);
SeqListPushFront(&s1, 10);
SeqListPushFront(&s1, 20);
SeqListPushFront(&s1, 30);
SeqListPrint(&s1);
SeqListPopFront(&s1);
SeqListPopFront(&s1);
SeqListPrint(&s1);
}
void TestSeqList3() {
SL s1;
SeqListInit(&s1);
SeqListPushFront(&s1,5);
SeqListPushFront(&s1,4);
SeqListPushFront(&s1, 3);
SeqListPushFront(&s1, 2);
SeqListPushFront(&s1, 1);
SeqListPrint(&s1);
SeqListInsert(&s1, 2, 30);
int pos = SeqListFind(&s1, 4);//先找到再插入
if (pos != -1) {
SeqListInsert(&s1, pos, 40);
}
SeqListPrint(&s1);
SeqListErase(&s1, 5);//删除下标为五的元素
SeqListPrint(&s1);
SeqListPopBack(&s1);//尾删
SeqListPrint(&s1);
}
int main() {//顺序表
//TestSeqList1();
//TestSeqList2();
TestSeqList3();
return 0;
}