目录
一、数据结构
1.1概念
1.2总结
1.3为什么需要数据结构?
二、顺序表
1.顺序表的概念及结构
1.1线性表
2.顺序表分类
2.1顺序表和数组的区别
2.2顺序表的分类
2.2.1静态顺序表
2.2.1.1概念
2.2.1.2缺陷
2.2.2动态顺序表
三、动态顺序表的实现
3.1新建项目
3.2 SeqList.h
3.3SeqList.c
3.3.1初始化
3.3.2销毁
3.3.3打印
3.3.4扩容
3.3.4.1扩容原则选择
3.3.4.2扩容代码呈现
3.3.5尾插
3.3.6头插
3.3.7尾删
3.3.8头删
3.3.9指定位置之前插入数据
3.3.10指定位置之前删除数据
3.3.11查找
3.3.12 SeqList.c
空间给少了不够⽤,给多了造成空间浪费
//C语言
#pragma once
#include
#include
#include
typedef int SLDataType;//不仅限于int类型 便于后续替换
//动态顺序表
typedef struct SeqList
{
SLDataType* arr; //存储数据的底层结构
int capacity; //记录顺序表的空间大小
int size; //记录顺便当前有效的数据个数
}SL;
//初始化和销毁
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);
//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDataType x);
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SLDestroy(SL* ps)
{
free(ps->arr);
ps->arr = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
//扩容
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* temp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (temp == NULL)
{
perror("realloc fail!");//扩容失败
exit(1);//退出
}
//扩容成功
ps->arr = temp;
ps->capacity = newCapacity;
}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);//断言,确保不为空
//空间不够直接插入
SLCheckCapacity(ps);
//空间足够直接插入
ps->arr[ps->size] = x;
ps->size++;
}
//头插
void SLPushFront(SL* ps, SLDataType 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 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 - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->arr);
SLCheckCapacity(ps);
//pos及之后的数据挪动一位
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(ps >= 0 && pos < ps->size);
for (int i = pos;i < ps->size;i++)
{
ps->arr[i] = ps->arr[i + 1];
}
}
int SLFind(SL* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
return i;
}
}
return -1; // 表示未找到
}
#include"SeqList.h"
//初始化和销毁
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SLDestroy(SL* ps)
{
free(ps->arr);
ps->arr = NULL;
ps->size = 0;
ps->capacity = 0;
}
//打印
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
//扩容
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* temp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (temp == NULL)
{
perror("realloc fail!");//扩容失败
exit(1);//退出
}
//扩容成功
ps->arr = temp;
ps->capacity = newCapacity;
}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);//断言,确保不为空
//空间不够直接插入
SLCheckCapacity(ps);
//空间足够直接插入
ps->arr[ps->size] = x;
ps->size++;
}
//头插
void SLPushFront(SL* ps, SLDataType 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 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 - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->arr);
SLCheckCapacity(ps);
//pos及之后的数据挪动一位
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(ps >= 0 && pos < ps->size);
for (int i = pos;i < ps->size;i++)
{
ps->arr[i] = ps->arr[i + 1];
}
}
int SLFind(SL* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
return i;
}
}
return -1; // 表示未找到
}