线性表
- 线性表是n个具有相同特性的数据元素的有限序列。
- 线性表是一种在实际中广泛使用的数据结构
- 常见的线性表:顺序表、链表、栈、队列、字符串…
- 线性表在逻辑上是线性结构,也就说是连续的一条直线。
- 在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储
顺序表
概念
- 用一段物理地址连续的存储单元
- 依次存储数据元素的线性结构,一般情况下采用数组存储。
- 在数组上完成数据的增删查改
一般可以分为
- 静态顺序表:使用定长数组存储元素
#define N 1000
typedef int SLDataType;
typedef struct SeqList //对类型进行重命名,方便后续变换类型
{
SLDataType array[N]; //定长数组
size_t size; //有效数据个数
}SL;
- 动态顺序表:使用动态开辟的数组存储
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* array; //指向动态开辟的数组
size_t size; //有效数据个数
size_t capacity; //容量空间的大小
}SL;
![![[Pasted image 20240810054754.png]]](https://i-blog.csdnimg.cn/direct/3cd9cba939414970a09bac9e951fec6c.png)
初始化
- 每次调用接口,要判断传入的指针是不是空指针
void SLInit(SL* ps)
{
assert(ps);
ps->a = (SLDataType*)malloc(sizeof(SLDataType)*4);
if (ps->a == NULL)
{
perror("malloc failed");
exit(-1); //程序直接在这里结束
}
ps->size = 0;
ps->capacity = 0;
}
- 动态内存,用malloc开辟空间,先开4个
- 检查malloc出的地址是否为空指针
- 将size和capacity初始化为0
销毁
void SLDestroy(SL* ps)
{
assert(ps);
free(ps-> a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
- 直接释放掉malloc的空间,将a指针置为NULL
- 将size和capacity置为0
打印
void SLPrint(SL* ps)
{
assert(ps);
for (int i = 0; i < ps-> size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
- 从下标0开始往右遍历,直到最后一个元素,即size-1
扩容
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
}
- 当size等于capacity时,realloc,重新开辟空间,一般扩2倍容量
尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
ps->a[ps->size] = x;
ps->size++;
}
- 在下标size处插入值,size++,如果size等于capacity,就扩容
尾删
void SLPopBack(SL* ps)
{
assert(ps);
//if (ps->size == 0)
// return;
assert(ps->size > 0);
ps->size--;
}
- 直接size–,覆盖掉尾部的元素
- 断言size,size不能减为负数
头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
}
需要整体元素往右挪动
而且只能从右往左开始遍历,反过来会覆盖没有处理的元素
end从size-1开始
![![[Pasted image 20240810060526.png]]](https://i-blog.csdnimg.cn/direct/e9f041d1936c4a6d92fd3372df0a062a.png)
把当前元素往右移动一格,传给end+1
![![[Pasted image 20240810061642.png]]](https://i-blog.csdnimg.cn/direct/f9ab1fd0ef2e4117b6982de7b095e1a1.png)
end–,往前一格
![![[Pasted image 20240810060703.png]]](https://i-blog.csdnimg.cn/direct/60f66a3a6811402dbadd7708bacd663e.png)
直到end=0
![![[Pasted image 20240810060808.png]]](https://i-blog.csdnimg.cn/direct/42fb2c83985342e086b49132dc8ffb94.png)
把x传进来,size++
![![[Pasted image 20240810060931.png]]](https://i-blog.csdnimg.cn/direct/299b6c7c714e41e9b5ac021ca87b4775.png)
当size=capacity时,就需要扩容
头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;
}
从左往右,挨个往左移动
begin从下标1开始
![![[Pasted image 20240810061444.png]]](https://i-blog.csdnimg.cn/direct/d77feeeb40964786a8ad521b80d7581e.png)
把begin的内容覆盖给begin-1
![![[Pasted image 20240810062211.png]]](https://i-blog.csdnimg.cn/direct/075b41286cc544c0ba79160466a94079.png)
begin++
![![[Pasted image 20240810061551.png]]](https://i-blog.csdnimg.cn/direct/dff1819a288544cbb147fbc4566683bb.png)
直到begin=size-1
![![[Pasted image 20240810062002.png]]](https://i-blog.csdnimg.cn/direct/99e7de537e8d468f838c3ddf2c199da9.png)
size–
![![[Pasted image 20240810062046.png]]](https://i-blog.csdnimg.cn/direct/862142de960d40bd8244954bdb9f26d2.png)
查找
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
- 依次遍历每个元素,找到以后,返回下标,否则返回-1
插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
- 先判断pos是否合法,pos可以等于size,因为可以尾插
end从size-1开始
![![[Pasted image 20240810064522.png]]](https://i-blog.csdnimg.cn/direct/3eeee134f226457e9b391e0186e14b2a.png)
当end大于等于pos时,end开始循环
将end传给end+1
![![[Pasted image 20240810064811.png]]](https://i-blog.csdnimg.cn/direct/cecc07109aaf4d9b988d082802e95f3b.png)
end–,往左移动
![![[Pasted image 20240810064743.png]]](https://i-blog.csdnimg.cn/direct/372cff074f17457791666b5597e21c65.png)
直到end=pos
![![[Pasted image 20240810064858.png]]](https://i-blog.csdnimg.cn/direct/fb7d1dc548944f01b30831d74464fdca.png)
将x传给pos,size++
![![[Pasted image 20240810064941.png]]](https://i-blog.csdnimg.cn/direct/377630d2ed9242dc96abcdad76a5907b.png)
当pos直接等于size时,相当于尾插
等于0时,相当于头插
删除
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--;
}
- 同样判断pos是否合法
begin从pos+1开始
![![[Pasted image 20240810065127.png]]](https://i-blog.csdnimg.cn/direct/e51240bc9bb54d1a95ca0a20f1787338.png)
将begin传给begin-1
![![[Pasted image 20240810065200.png]]](https://i-blog.csdnimg.cn/direct/80c4bb44fe174e42aa90b155c8bf97bc.png)
begin++
![![[Pasted image 20240810065219.png]]](https://i-blog.csdnimg.cn/direct/14a76f27e56c41f5bf31acba37151322.png)
直到begin=size-1
![![[Pasted image 20240810065252.png]]](https://i-blog.csdnimg.cn/direct/89dd254ea159445b8ae81cdfee436e51.png)
size–
![![[Pasted image 20240810065322.png]]](https://i-blog.csdnimg.cn/direct/2462c1456f1545aeb8fd8388ed9c278f.png)
当pos等于size-1时,相当于尾删
等于0时,相等于头删
修改
void SLModify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
- 判断pos的合法性,直接通过数组下标修改
声明定义分离实现
//管理数据 -- 增删查改
//初始化
void SLInit(SL* ps);
//销毁
void SLDestroy(SL* ps);
//打印
void SLPrint(SL* ps);
//扩容
void SLCheckCapacity(SL* ps);
//尾插
void SLPushBack(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头插
void SLPushFront(SL* ps, SLDataType x);
//头删
void SLPopFront(SL* ps);
//查找
int SLFind(SL* ps, SLDataType x);
//插入
void SLInsert(SL* ps, int pos, SLDataType x);
//删除
void SLErase(SL* ps, int pos);
//修改
void SLModify(SL* ps, int pos, SLDataType x);
#include "SeqList.h"
void SLInit(SL* ps)
{
assert(ps);
ps->a = (SLDataType*)malloc(sizeof(SLDataType)*4);
if (ps->a == NULL)
{
perror("malloc failed");
exit(-1); //程序直接在这里结束
}
ps->size = 0;
ps->capacity = 0;
}
void SLDestroy(SL* ps)
{
assert(ps);
free(ps-> a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
void SLPrint(SL* ps)
{
assert(ps);
for (int i = 0; i < ps-> size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
}
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
ps->a[ps->size] = x;
ps->size++;
}
void SLPopBack(SL* ps)
{
assert(ps);
//if (ps->size == 0)
// return;
assert(ps->size > 0);
ps->size--;
}
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
}
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;
}
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
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--;
}
void SLModify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}



















