目录
一、什么是数据结构
概念:数据结构是计算机存储、组织数据的方式。具体来说,它是指相互之间存在一种或多种特定关系的数据元素的集合。数据结构反映数据的内部构成,即数据由那部分构成,以什么⽅式构成,以及数据元素之间呈现的结构。
数据结构包含数据的逻辑结构、物理结构以及它们之间的相互关系,并定义了相应的运算,设计了相应的算法。
常见的数据结构包括数组、栈、链表、队列、树、图、堆和散列表等。这些数据结构在不同的场景和应用中具有不同的优势和用途。
最基础的数据结构:数组
线性表:
线性表是一种线性数据结构,它用一组地址连续的存储单元依次存储线性表的数据元素。在数据结构中,线性表是一个最基本、最简单、也是最常用的一种数据结构。常见的线性表:顺序表、链表、栈、队列、字符串...
顺序表:
定义:顺序表是用一段地址连续的存储单元依次存储线性表的数据元素。在顺序表中,数据元素之间的逻辑关系和物理位置是相邻的。顺序表具有简单易实现的优点,只要确定了存储顺序表的起始位置,表中任一元素的地址都可以通过简单的计算得到。
顺序表采用一维数组来存储元素,数组中的元素在内存中按照一定的顺序连续存放。顺序表的基本操作主要包括插入、删除、查找、读取和修改等。
顺序表的分类
静态顺序表
.使用定长数组来存储元素。
.在编译时分配内存空间,一旦分配后,其大小就不能再改变。
.如果在实际使用中,元素个数少于分配的空间,则会造成空间的浪费;如果元素个数超过分配的.空间,则会发生数组越界错误。
.因此,静态顺序表只适用于确定知道需要存储多少数据的场景。
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE]; // 存放顺序表元素的数组
int len; // 顺序表当前长度
} SeqList;
动态顺序表
.使用动态开辟的数组(通常在堆上)来存储元素。
.在运行时根据需要动态地分配或回收存储空间,使得表的大小可以按需增减。
.动态顺序表更加灵活,能够适应元素个数动态变化的情况。
.在实际应用中,动态顺序表被广泛使用,因为它可以根据需要动态地调整存储空间的大小,避免了空间的浪费和数组越界的问题。
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;
int size; // 有效数据个数
int capacity; // 空间容量
}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);//pos位置插入数据
void SLErase(SL* ps, int pos);//指定位置删除
int SLFind(SL* ps, SLDataType x);//查找
二、顺序表功能的实现
初始化
void SLInit(SL* ps)
{
assert(ps);
ps->arr = (SLDataType*)malloc(sizeof(SLDataType) * 4); // 初始容量设为4
if (ps->arr == NULL)
{
perror("malloc fail");
exit(1); // 如果malloc失败,则退出程序
}
ps->size = 0;
ps->capacity = 4;
}
销毁
void SLDestroy(SL* ps)
{
assert(ps);
free(ps->arr);
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
打印数据
void SLPrint(SL* ps)
{
assert(ps);
if (ps->size == 0)
{
return;
}
int i = 0;
for (; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
检查空间扩容
//扩容
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int new = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, new * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(1);
}
ps->arr = tmp;
ps->capacity = new;
}
}
尾插
首先,需要检查顺序表是否还有足够的空间来插入新的元素。如果当前顺序表已满,则需要进行扩容操作。
在顺序表的末尾插入新的元素。
插入元素后,需要更新顺序表的大小。
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size] = x;
ps->size++;
}
尾删
检查空表:需要检查顺序表是否为空。如果为空,则不能进行尾删操作。
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size > 0);
ps->size--; //减少size,从而移除最后一个元素
}
头插
检查空间:首先,需要检查顺序表是否还有足够的空间来插入新的元素。如果当前顺序表已满,则需要进行扩容操作。
元素后移:将元素向后移动。
插入元素:在顺序表的开头插入新的元素。将插入数据赋值给顺序表的第一个位置即arr[0]来实现。
更新大小:插入元素后,需要更新顺序表的大小。。
//头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
for (int i = ps->size - 1; i >= 0; i--)// 从数组的末尾开始,将所有元素向后移动一个位置
{
ps->arr[i + 1] = ps->arr[i];// 将元素向后移动
}
ps->arr[0] = x;// 在数组的开头放置新元素
ps->size++;// 增加size
}
头删
检查空表:检查顺序表是否为空。如果为空,则不能进行头删操作。
移动元素:将顺序表中从第二个元素开始的所有元素向前移动一个位置,以覆盖第一个元素。
更新大小:将顺序表的大小减一。
//头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size > 0);
for (int i = 1; i < ps->size; i++) // 从第二个元素开始,向前移动每个元素
{
ps->arr[i - 1] = ps->arr[i]; // 将后面的元素向前移动一个位置
}
ps->size--;
}
指定位置指定插入
检查边界和容量:检查指定的位置是否在范围之内,以及顺序表是否有足够的空间来插入新元素。如果顺序表已满,需要扩容。
移动元素:从指定位置开始,将后续的所有元素向后移动一个位置,为新元素腾出空间。
插入元素:在指定的位置插入新元素。
更新大小:将顺序表的大小加一。
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos <= ps->size&&pos>=0);
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--; // 更新size,减少一个元素
}
查找
//查找
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)//循环遍历顺序表中的元素
{
if (ps->arr[i] == x)
{
return i;。。// 返回当前元素的下标
}
}
return -1;
}
三、完整源码:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct SeqList
{
SLDataType* arr;
int size; // 有效数据个数
int capacity; // 空间容量
}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)
{
assert(ps);
ps->arr = (SLDataType*)malloc(sizeof(SLDataType) * 4); // 初始容量设为4
if (ps->arr == NULL)
{
perror("malloc fail");
exit(1); // 如果malloc失败,则退出程序
}
ps->size = 0;
ps->capacity = 4;
}
//销毁
void SLDestroy(SL* ps)
{
assert(ps);
free(ps->arr);
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
//打印
void SLPrint(SL* ps)
{
assert(ps);
if (ps->size == 0)
{
return;
}
int i = 0;
for (; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
//扩容
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int new = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, new * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(1);
}
ps->arr = tmp;
ps->capacity = new;
}
}
//尾插
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 - 1; i >= 0; i--)// 从数组的末尾开始,将所有元素向后移动一个位置
{
ps->arr[i + 1] = ps->arr[i];// 将元素向后移动
}
ps->arr[0] = x;// 在数组的开头放置新元素
ps->size++;// 增加size
}
//头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size > 0);
for (int i = 1; i < ps->size; i++) // 从第二个元素开始,向前移动每个元素
{
ps->arr[i - 1] = ps->arr[i]; // 将后面的元素向前移动一个位置
}
ps->size--;
}
pos位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos <= ps->size&&pos>=0);
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--; // 更新size,减少一个元素
}
//查找
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
return i;
}
}
return -1;
}