一、什么是顺序表
顺序表(Sequence List)是一种常见的数据结构,用于存储线性表中的元素。线性表是一种数据结构,其中的元素排列成一条直线,具有前驱和后继的关系。顺序表通过一段连续的存储单元来存储线性表中的元素,并且元素之间的顺序与其在存储空间中的位置一致。
在顺序表中,元素的存储通常采用数组来实现。数组是一种连续存储的数据结构,通过索引来访问元素。在顺序表中,每个元素在数组中占据一个位置,并且可以通过元素的下标(索引)来直接访问该元素。
顺序表具有以下特点:
- 内存连续:顺序表中的元素在内存中是连续存储的,这样就可以通过计算偏移量来访问任意位置的元素,使得访问效率高。
- 随机访问:由于元素在内存中的位置是连续的,并且通过下标直接访问,因此可以实现随机访问,即可以在常数时间内访问任意位置的元素。
- 插入和删除效率较低:由于顺序表中的元素是连续存储的,插入和删除元素可能需要移动大量元素,因此其效率较低,特别是在中间位置进行插入和删除操作时。
- 容量固定:一般情况下,顺序表的容量是固定的,即在创建顺序表时需要指定其最大容量,当顺序表存储的元素数量达到容量上限时,需要进行扩容操作。
顺序表一般分为两大类:
1.静态顺序表
概念:使⽤定⻓数组存储元素
静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费
2.动态顺序表
概念:使用动态开辟的数据存储
二、动态顺序表实现
学习目标:
//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps); //保持接口一致性
//顺序表的头部/尾部插入
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); //查找
void SLModify(SL* ps,int pos, SLDataType x); //修改
1.创建
"头文件" 文件夹中创建 SeqList.h 用来存放头文件。在 "源文件" 文件夹中创建 SeqList.c 用来实现函数,Test.c 用来测试我们的顺序表。
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//静态顺序表
//#define N 100
//struct SeqList
//{
// SLDataType a[N];
// int size;
//};
//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr; //存储数据的底层结构
int capacity; //记录顺序表的空间大小
int size; //记录顺序表当前有效的数据个数
}SL;
2.初始化
void SLInit(SL* ps) {
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
3.扩容
void SLCheckCapacity(SL* ps) {
if (ps->size == ps->capacity) {
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (tmp == NULL) {
perror("realloc fail!");
exit(1);
}
//扩容成功
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
4.尾插
void SeqListPushBack(SL* ps, SLDateType x) {
assert(ps);
SeqListCheck(ps);
ps->a[ps->size] = x;
ps->size++;
}
5.打印
void SLPrint(SL* ps) {
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
6.销毁
void SeqListDestroy(SL* ps) {
assert(ps);
free(ps->a);//在顺序表中,存储数据的空间是开辟出来的,所以摧毁时要free开辟出的空间
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
7.尾删
void SLPopBack(SL* ps) {
assert(ps);
assert(ps->size);
//顺序表不为空
ps->size--;
}
8.头插
void SLPushFront(SL* ps, SLDataType x) {
assert(ps);
//判断是否扩容
SLCheckCapacity(ps);
//旧数据往后挪动一位
for (int i = ps->size; i > 0; i--) //i = 1
{
ps->arr[i] = ps->arr[i - 1]; //ps->arr[1] = ps->arr[0]
}
ps->arr[0] = x;
ps->size++;
}
9.头删
void SLPopFront(SL* ps) {
assert(ps);
assert(ps->size);
//不为空执行挪动操作
for (int i = 0; i < ps->size-1 ;i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
10.插入任意位置
//指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x) {
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
//pos及之后的数据往后挪动一位,pos空出来
for (int i = ps->size; i > pos ;i--)
{
ps->arr[i] = ps->arr[i - 1]; //ps->arr[pos+1] = ps->arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
11.删除任意位置
//删除指定位置数据
void SLErase(SL* ps, int pos) {
assert(ps);
assert(pos >= 0 && pos < ps->size);
//pos以后的数据往前挪动一位
for (int i = pos;i < ps->size-1;i++)
{
ps->arr[i] = ps->arr[i + 1];//ps->arr[i-2] = ps->arr[i-1];
}
ps->size--;
}
12.查找
int SeqListFind(SL* ps, SLDateType x,int begin) {
assert(ps);
//int i=begin,意在从下标为i的数据开始往后查找
for (int i = begin; i < ps->size; i++) {
if (ps->a[i] == x) {
return i;//返回该数据在数组中的下标
}
}
return -1;//如果未找到返回负一(“暴力”的方式)
}
13.修改
//修改
void SLModify(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}