一、线性表
二、顺序表
(一)顺序表的概念
概念:顺序表是一段用物理地址连续的存储单元依次存储数据元素的线性结构,那顺序表和数组的区别是什么?
顺序表的底层结构是数组,对数组的封装,实现了常见的增删查改等接口。
换句话说,数据结构完成的就是顺序表的增删查改等接口函数的实现。

(二)顺序表的分类
1、静态顺序表
静态顺序表就是使用定长数组存储元素。
静态顺序表缺陷:空间给少了不够用,给多了造成空间浪费。
2、动态顺序表
动态顺序表就是按需申请空间存储进行存储。
(三)顺序表的结构


(四)动态顺序表的实现
头文件
#include<stdio.h>
#include<stdlib.h>//动态内存管理的头文件
#include<assert.h>//用于检查报错
1. 定义顺序表
//定义动态顺序表
typedef int SLDatatype;
typedef struct Seqlist{
SLDatatype* arr;
int size; //数据个数
int capacity;//空间容量
}SL;
2需要实现的功能
void SLInit(SL* ps);//初始化
void SLCheckCapacity(SL* ps);//增容
void SLPushBack(SL* ps, SLDatatype x);//尾插
void arrprint(SL* ps);//打印数组;
void SLPushFront(SL* ps, SLDatatype x);//头插;
void SLPopBack(SL* ps);//尾删
void SLPopFront(SL* ps);//头删
void SLInsert(SL* ps, int pos, SLDatatype x);//在下标为pos的位置插入
void SLErase(SL* ps, int pos);//删除下标为pos
int SLFind(SL* ps, SLDatatype x);//查找值为x的下标
void SLDestory(SL* ps);
3.初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
先将所有的数据置空
4.顺序表的动态增容
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 failed");
exit(1);
}
ps->arr = tmp;
ps->capacity = newcapacity;
}
}
代码讲解&注释:
-
触发扩容的条件:
if (ps->size == ps->capacity)当顺序表中已存储的元素数量(
size)等于当前最大容量(capacity)时,说明存储空间已满,必须扩容才能继续插入新元素。这是防止数组越界的关键判断。 -
扩容策略的选择
int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;初始状态(
capacity == 0)时,直接分配初始容量(4 个元素),避免2*0=0的无效扩容。非初始状态时,采用「2 倍扩容」策略:平衡了性能和内存利用率。若每次只扩 1 个容量,频繁插入会导致多次内存分配(时间开销大);若扩容倍数过大,则会浪费内存。 -
安全的内存分配方式
SLDatatype* tmp = (SLDatatype*)realloc(ps->arr, newcapacity * sizeof(SLDatatype));使用
realloc而非malloc:realloc可在原有内存块基础上扩展(若相邻地址有足够空间),避免额外的内存拷贝。用临时指针tmp接收结果:若realloc失败(返回NULL),不会覆盖原数组指针ps->arr,防止内存泄漏。 -
异常处理
if (tmp == NULL) { perror("realloc failed"); exit(1); }内存分配可能失败(如内存不足),必须检查返回值。若失败则打印错误信息并退出程序,避免后续使用
NULL指针操作内存导致崩溃。 -
更新状态
ps->arr = tmp; ps->capacity = newcapacity;只有当内存分配成功后,才更新数组指针和容量,确保顺序表状态的一致性(容量与实际内存匹配)。
尾插&头插
void SLPushBack(SL* ps, SLDatatype x)
{
assert(ps != NULL);
SLCheckCapacity(ps);
//空间足够
ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, SLDatatype x)
{
assert(ps != NULL);
SLCheckCapacity(ps);
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
代码讲解&注释
1. 尾插操作
ps->arr[ps->size++] = x
顺序表的末尾位置就是 size 指向的下标(因为 size 表示当前元素个数,最后一个元素下标为 size-1),直接通过下标访问赋值,时间复杂度为 O(1)。size++ 是后置自增,先使用当前 size 作为下标,插入后再将元素个数加 1,确保状态正确。
2. 头插操作
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
- 顺序表的内存是连续的,第一个位置(下标 0)已有元素时,必须先将所有元素从后往前依次后移一位,才能腾出下标 0 的位置。
3.顺序表的底层特性
- 顺序表基于连续的数组内存实现,元素在内存中连续存储。
- 优势:支持随机访问(通过下标直接访问,时间复杂度 O (1)),适合尾插、按位置访问等操作。
- 劣势:中间 / 头部插入删除需要移动元素(时间复杂度 O (n)),因为要保持内存连续性。
尾删&头删
void SLPopFront(SL* ps)
{
assert(ps && ps->size);
for (int i = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i+1];
}
ps->size--;
}
void SLPopBack(SL* ps)
{
assert(ps && ps->size);
ps->size--;
}
代码讲解&注释:
- 头删时必须从前往后移(
i从 0 递增),确保前面的位置被后面的元素依次覆盖。 - 头删的循环终止条件
i < ps->size - 1必须严格遵守:若写成i < ps->size,会导致越界访问(当i = size-1时,arr[i+1]是arr[size],超出原有效范围)。
在下标为pos的位置插入&删除下标为pos
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);
//pos之后的数据整体向后移动一位;
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i+1];
}
ps->size--;
}
代码讲解&注释:
插入后移:i 从 size 到 pos+1 「从后往前移」
删除前移:i 从 pos 到 size-2 「从前往后移」
与其他操作的关系
- 头插
SLPushFront等价于SLInsert(ps, 0, x)。 - 尾插
SLPushBack等价于SLInsert(ps, ps->size, x)。 - 头删
SLPopFront等价于SLErase(ps, 0)。 - 尾删
SLPopBack等价于SLErase(ps, ps->size-1)。
| 操作 | 时间复杂度 | 优势场景 | 局限场景 |
|---|---|---|---|
| 随机访问 | O(1) | 按下标读取 / 修改(如arr[i]) | 无(这是顺序表核心优势) |
| 尾部插入 / 删除 | O(1) | 频繁在末尾添加 / 删除元素 | - |
| 中间 / 头部插入 / 删除 | O(n) | - | 频繁在中间或头部操作 |
查找值为x的下标(返回下标index)
int SLFind(SL* ps, SLDatatype x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
printf("找到了");
return i;
}
}
printf("没找到");
return -1;
}
数据遍历打印
void arrprint(SL* ps) {
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
数据的销毁
void SLDestory(SL* ps)
{
if (ps->arr)
free(ps->arr);
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
代码的测试和结果显示:
#pragma once
#include"Sequlist.h"
void test01()
{
SL sl;
SLInit(&sl);
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
arrprint(&sl);
SLPushFront(&sl, 5);
SLPushFront(&sl, 4);
SLPushFront(&sl, 3);
arrprint(&sl);
SLPopBack(&sl);
arrprint(&sl);
SLPopFront(&sl);
arrprint(&sl);
SLInsert(&sl, 1, 2);
arrprint(&sl);
int find = SLFind(&sl, 222);
SLDestory(&sl);
}
int main()
{
test01();
return 0;
}


3040





