一、线性表
顺序表是线性表的一种
线性表(linear list)是n个具有相同特性的数据元素的有限序列(集合)。线性表是⼀种在实际中⼴泛使⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储
二、顺序表的分类
顺序表的底层结构就是数组,对数组的封装,实现了常用的增删改查等接
2.1静态顺序表
使用定长数组去储存元素
但是这里有个问题:因为是定长,所以空间给少了不够用,给多了又浪费
2.2动态顺序表
因为可以根据自身需求动态申请空间,所以动态是不是就更好一点呀
三、动态顺序表的实现
那么接下来我们看一下是怎么实现的吧
首先,为了让自身代码看起来更清晰,我们可以创造三个文件
头文件:SeqList.h //用来放文件的声明已经类型的定义
源文件:SeqList.c //实现顺序表的方法
text.c //测试代码
直接在头文件声明结构体, 并且进行一些函数的声明,我把所有声明都放在下面了
//SeqList.c
#pragma once //以下声明只会包含一次, 提高代码效率
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
typedef int SLDataType;//将数据类型重命名,方便后期修改
//动态顺序表
typedef struct SeqList
{
SLDataType* arr;
int size; //有效数据个数
int capacity; //空间大小
}SL;
//顺序表初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
void SLPrint(SL s);//打印内容
//尾部插入 头部插入
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);
//尾删 / 头删
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
接下来就可以封装各种函数了
首先是初始化:
void SLInit(SL* ps)
{
assert(ps);//断言以下防止传个NULL
ps->arr = NULL:
ps->size = ps->capacity = 0;
}
销毁:释放空间,防止内存泄漏
void SLDestroy(SL* ps)
{
assert(ps);
if(ps->arr)
{//如果ps->arr有内容就释放掉这块内存
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
先把准备工作做好,就可以上高速了
在插入前我们要先想一想怎么插入呢,怎么才能知道空间够不够呢?所以要先判断空间内存大小,为了方便后期使用我们可以封装成一个函数
void SLCheckCapacity(SL* ps)
{
assert(ps);
if(ps->size == ps->capacity)//没空间或者满了,就需要扩容
{
int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
//申请多大空间
SeqDataType* tmp = (SeqDataType*)realloc(ps->arr,NewCapacity * szieof(SeqDataType));
}
if (tmp == NULL)
{
perror("realloc fail");
exit(1);
}
ps->arr = tmp;
ps->capacity = newCapacity;
}
尾插:
void SLPushBack(SL* ps,SeqDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
//后置++,先使用后++
}
头插:将整体数据向后移动一位,就可以插入了
void SLPushBack(SL* ps,SeqDataType x)
{
assert(ps);
SLCheckCapacity(ps);
//将已有数据整体向后移动一位
for (int i = ps->size; i>0; i--)
{
ps->arr[i] = ps->arr[i-1];
}
ps->arr[0] = x;
ps->size++;
}
写完头插尾插可以测试一下,看看自己写的对不对,为了更直观的看到可以,封装一个打印函数
void SLPrint(SL ps)
{
for(int i = 0; i < ps.size;i++)
{
printf("%d",ps.arr[i];
}
printf("\n"):
}
在测试源文件text.c里写
#include "Seqlist.h"
int main()
{
SL s;
SLInit(&s);
//尾插
SLPushBack(&s, 4);
SLPushBack(&s, 3);
SLPushBack(&s, 2);
SLPushBack(&s, 1);
SLprint(s);
//头插
SLPushFront(&s, 3);
SLPushFront(&s, 4);
SLprint(s);
return 0;
}
接下来,实现删除操作
尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
头删:头删就和头插很类似,仿佛反过来了
数据整体向前挪动一位
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
for(int i = 0;i < ps->size-2 ;i++)
{ //因为最后一次是arr[size-2] = arr[size-1]
ps->arr[i] = ps->arr[i+1];
}
ps->size--;
}
依然可以像之前一样,测试一下
#include "Seqlist.h"
int main()
{
SL s;
SLInit(&s);
//尾插
SLPushBack(&s, 4);
SLPushBack(&s, 3);
SLPushBack(&s, 2);
SLPushBack(&s, 1);
SLprint(s);
//头插
SLPushFront(&s, 3);
SLPushFront(&s, 4);
SLprint(s);
SLPopBack(&s);
SLprint(s);
SLpopFront(&s);
SLprint(s);
SLDestory(&s);
return 0;
}
写到这里就算实现了,接下来可延伸一点 指定位置之前插入/删除数据
到这里我们就算写完了,是不是感觉很容易呢
四、顺序表实现完整代码
Seqlist.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;
int size; //有效数据个数
int capacity; //空间大小
}SL;
//顺序表初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
void SLPrint(SL s);
//头部插入 尾部插入
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);
//头删 / 尾删
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDataType x);
Seqlist.c
#include"Seqlist.h"
void SLInit(SL* ps)
{
assert(ps);
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
void SLDestory(SL* ps)
{
assert(ps);
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SeqDataType* tmp = (SeqDataType*)realloc(ps->arr, Newcapacity * sizeof(SeqDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(1);
}
ps->arr = tmp;
ps->capacity = Newcapacity;
}
}
//尾插
void SLPushBack(SL* ps, SeqDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, SeqDataType x)
{
assert(ps);
SLCheckCapacity(ps);
for (int i = ps->size; i>0; i--)
{
ps->arr[i] = ps->arr[i-1];//最后一次ps->arr[1] = ps->arr[0]
}
ps->arr[0] = x;
ps->size++;
}
void SLprint(SL ps)
{
for (int i = 0; i < ps.size; i++)
{
printf("%d ", ps.arr[i]);
}
printf("\n");
}
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
void SLpopFront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 0; i <=ps->size-2 ; i++)
{
ps->arr[i] = ps->arr[i + 1];//arr[size-2] = arr[size-1]
}
ps->size--;
}
//指定位置之前删除数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = x;
ps->size++;
}
//指定位置之前插入数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
text.c
#include "Seqlist.h"
int main()
{
SL s;
SLInit(&s);
SLPushBack(&s, 4);
SLPushBack(&s, 3);
SLPushBack(&s, 2);
SLPushBack(&s, 1);
SLprint(s);
SLPushFront(&s, 3);
SLPushFront(&s, 4);
SLprint(s);
SLPopBack(&s);
SLprint(s);
SLpopFront(&s);
SLprint(s);
SLInsert(&s, 2, 666);
SLInsert(&s, s.size, 999);
SLErase(&s, 1);
SLPrint(sl);
SLDestory(&s);
return 0;
}
顺序表是一种经典的线性数据结构,具有高效的存储和访问性能。但在插入和删除操作上稍显不足,适用于元素数量固定且不经常变动的场景
本篇到这里就结束了,如果写的有问题欢迎在评论区指导,下次再见