顺序表的实现
线性表
**** 线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。****

顺序表
** 顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。**
顺序表的定义
1.静态定义:默认声明长度是多大
#define N 100
typedef int SLDataType;
struct SeqList
{
SLDataType a[N];
int size;
};
2.动态定义:使用mallo关键字进行动态开辟内存
typedef int SLDatType;
typedef struct SeqList
{
SLDatType* a;//数组a
int size;//控制数组下标
int capacity;//数组容量
}SL;
顺序表的初始化
void SLInit(SL* ps)
{
assert(ps);//断言传过来的是否是指针
ps->a = (SLDatType*)malloc(sizeof(SLDatType) * 4);//扩容
if (ps->a == NULL)
{
perror("malloc filed");
exit(-1);
}
ps->size = 0;
ps->capacity = 4;
}
顺序表数据的增加
数组扩容函数
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDatType* tmp = (SLDatType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDatType)));//进行数组内存扩容
if (tmp == NULL)
{
perror("realloc failed");
}
ps->a = tmp;
ps->capacity *= 2;
}
1.头插法
数组尾部向头部挪动每个元素,且每个元素向后挪一个间距
void SLPushFront(SL* ps, SLDatType x)
{
SLCheckCapacity(ps);//检查数组容量
int end = ps->size - 1;//数组最后一个元素下标
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
}
2.尾插法
直接插入到数组尾部
void SLPushBack(SL* ps, SLDatType x)
{
SLCheckCapacity(ps);
ps->a[ps->size] = x;//数组最后一个元素下标是ps->size - 1,所以插入就在加+1
ps->size++;//插入后下标+1
}
3.按pos位插入
输入自己想要的的位置pos,在pos位后插入数据
void SLInsert(SL* ps, int pos, SLDatType x)
{
assert(ps);
assert(pos>=0&&pos<=ps->size);//必须在pos必须在数组,防止越界
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
顺序表的删除
1.头删
将数组所有数据往前覆盖
void SLPopFront(SL* ps)
{
assert(ps->size > 0);//必须数组里有元素
int begin = 1;//纪录下标位2的位置
while (begin <=ps->size-1 )
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}
2.尾删
void SLPopBack(SL* ps)
{
assert(ps->size>0);
//ps->a[ps->size-1] = 0;
ps->size--;
}
3.按pos位删除
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
int begin = pos + 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}
顺序表的查找
查找相当于遍历素组
//查找数据中有没有这个值,有返回下标
int SLFind(SL* ps, SLDatType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i]==x)
{
return i;
}
}
return -1;//找不到返回-1
}
顺序表的修改
void SLModify(SL* ps, int pos,SLDatType x)
{
SLFind(ps, pos);//找到返回下标
ps->a[pos] = x;
}
顺序表的销毁
void SLDestroy(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
打印顺序表
//打印顺序表
void SLPrint(SL* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
头文件的声明和测试各函数接口
typedef int SLDatType;
typedef struct SeqList
{
SLDatType* a;
int size;
int capacity;
}SL;
//管理数据--增删查改
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
void SLCheckCapacity(SL* ps);
//插头删 尾插尾删
void SLPushBack(SL* ps, SLDatType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDatType x);
void SLPopFront(SL* ps);
//按pos位插入和删除
void SLInsert(SL* ps, int pos, SLDatType x);
void SLErase(SL* ps, int pos);
//查找顺序表中的值
int SLFind(SL* ps, SLDatType x);
//修改顺序表的值
void SLModify(SL* ps, int pos,SLDatType x);
测试
#include"SeqList.h"
void text()
{
SL sl;
SLInit(&sl);
SLInsert(&sl, 2, 40);
}
int main()
{
SL sl;
SLInit(&sl);
SLPushBack(&sl,1);
SLPushBack(&sl,2);
SLPushBack(&sl,3);
SLPushBack(&sl, 4);
SLPushBack(&sl, 5);
SLPushBack(&sl, 6);
SLPrint(&sl);
SLPopBack(&sl);
SLPopBack(&sl);
SLPrint(&sl);
SLPushFront(&sl, 10);
SLPushFront(&sl, 20);
SLPushFront(&sl, 30);
SLPushFront(&sl, 40);
SLPrint(&sl);
SLPopFront(&sl);
SLPopFront(&sl);
SLPrint(&sl);
SLInsert(&sl, 2, 40);
SLPrint(&sl);
SLErase(&sl, 2);
SLPrint(&sl);
int n = 0;
scanf("%d", &n);
int ret=SLFind(&sl, n);
if (ret != -1)
{
SLInsert(&sl, ret, n*10);//ret位置插入
}
SLPrint(&sl);
return 0;
}
各接口测试结果




894




被折叠的 条评论
为什么被折叠?



