目录
顺序表的基本概念
接口实现
定义顺序表
始化顺初序表
增容
打印
销毁顺序表
顺序表尾插
测试
编辑
头插
测试
尾删
测试
头删
测试
查找
在指定位置插入数据
测试
删除指定位置的数据
测试
完整代码
Seqlist.h
SeqList.c
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般用数组存储,在数组上完成对对数据的增删查改
顺序表分为静态顺序表和动态顺序表
静态顺序表:使用定长数组存储数据
动态顺序表:使用动态开辟的数组存储数据
静态顺序表比较局限,在现实中我们一般研究动态顺序表对数据的存储
命名数据类型,方便下一次修改数据类型
如果顺序表中元素个数等于顺序表的最大容量就需要进行增容
#pragma once
#include
#include
#include
typedef int SLDataType;
//动态顺序表
typedef struct SeqList
{
SLDataType* array; //指向动态开辟的数组
int size; //顺序的元素个数
int capicity; //数组的容量大小
}SeqList;
void SeqListInit(SeqList* psl); //初始化顺序表
void CheckCapicity(SeqList* psl); //扩容
void SeqListPrint(SeqList* psl); //打印顺序表
void DestorySeqList(SeqList* psl); //销毁顺序表
void SeqListPushback(SeqList* psl,SLDataType x); //尾插
void SeqListPushfront(SeqList* psl, SLDataType x); //头插
void SeqListpopback(SeqList* psl); //尾删
void SeqListpopfront(SeqList* psl); //头删
int SeqListFind(SeqList* ps, SLDataType x); //查找
void SeqLIstInsert(SeqList* psl, SLDataType pos, SLDataType x); //在pos位置插入值
void SeqLIstErase(SeqList* psl, SLDataType pos); //删除pos位置的值
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void SeqListInit(SeqList* psl) //初始化顺序表
{
assert(psl); //断言,排查控指针
psl->array = (SLDataType*)malloc(sizeof(SLDataType) * 4); //动态开辟数组
if (psl->array == NULL) //开辟失败
{
perror("malloc failed");
exit(-1);
}
psl->size = 0; //元素为0
psl->capicity = 4; //容量为4
}
void CheckCapicity(SeqList* psl) //扩容
{
assert(psl); //断言
if (psl->size == psl->capicity) //元素个数等于容量,增容
{
SLDataType* tmp = (SLDataType*)realloc(psl->array, psl->capicity * 2 * sizeof(SLDataType)); //增容
if (tmp == NULL) //增容失败
{
perror("realloc failed");
exit(-1);
}
psl->array = tmp;
psl->capicity = psl->capicity * 2;
}
}
void SeqListPrint(SeqList* psl) //打印顺序表
{
assert(psl); //断言
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->array[i]);
}
printf("\n");
}
void DestorySeqList(SeqList* psl) //销毁顺序表
{
assert(psl); //断言
free(psl->array);
psl->array = NULL;
psl->size = psl->capicity = 0;
}
void SeqListPushback(SeqList* psl,SLDataType x) //尾插
{
assert(psl);
CheckCapicity(psl);
psl->array[psl->size] = x;
psl->size += 1;
}
void SeqListPushfront(SeqList* psl, SLDataType x) //头插
{
assert(psl);
CheckCapicity(psl);
/*for (int i = psl->size; i > 0; i--)
{
psl->array[i] = psl->array[i - 1];
}
psl->array[0] = x;*/
int end = psl->size - 1;
while (end >= 0)
{
psl->array[end + 1] = psl->array[end];
end--;
}
psl->array[0] = x;
psl->size += 1;
}
void SeqListpopback(SeqList* psl) //尾删
{
assert(psl);
if (psl->size == 0)
{
return;
}
psl->array[psl->size - 1] = 0;
psl->size--;
}
void SeqListpopfront(SeqList* psl) //头删
{
assert(psl);
if (psl->size == 0)
{
return;
}
int i = 0;
while (i < psl->size - 1)
{
psl->array[i] = psl->array[i + 1];
i++;
}
psl->size--;
}
int SeqListFind(SeqList* ps, SLDataType x) //查找
{
assert(ps);
for(int i=0;isize;i++)
{
if (ps->array[i] == x)
{
return i;
}
}
return -1;
}
void SeqLIstInsert(SeqList* psl, SLDataType pos, SLDataType x) //在pos位置插入值
{
assert(psl);
assert(pos <= psl->size&& pos >= 0);
CheckCapicity(psl);
int end = psl->size - 1;
while (end >= pos)
{
psl->array[end + 1] = psl->array[end];
end--;
}
psl->array[pos] = x;
psl->size += 1;
}
void SeqLIstErase(SeqList* psl, SLDataType pos) //删除pos位置的值
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
int begin = pos + 1;
while (begin < psl->size )
{
psl->array[begin - 1] = psl->array[begin];
begin++;
}
psl->size--;
}