数据结构——顺序表(静/动态代码实现)

静态顺序表

数据结构中顺序表意为一组集合数据所占的内存空间是连续的,且每个元素都与自己的下表一一对应。静态顺序表可采用数组的形式存储和进行元素的增删查改。

参考代码

头文件:

用于引用库函数,定义常变量、结构体,声明函数

cpp 复制代码
LList.h
#pragma once
#include<iostream>
#include<algorithm>

using namespace std;

#define ElemType int
#define MaxSize 100

typedef struct LNode {
	int data[MaxSize];
	int length;
}SqList;

//初始化
void InitList(SqList& L);

//打印
void PrintList(SqList L);

//插入
bool ListInsert(SqList& L, int i, ElemType e);

//删除
bool ListDelete(SqList& L, int i, ElemType& e);

//查找
int ListFind(SqList L, ElemType e);

//修改
bool ListModify(SqList& L, int i, ElemType e);

//销毁
void DestroyList(SqList& L);

实现文件

cpp 复制代码
LList.cpp
#include"LList.h"


//初始化(所有元素都为0,表长length为0)
void InitList(SqList& L) {
	for(int i =0;i<MaxSize;i++)
		L.data[i] = 0;
	L.length = 0;
}

//打印
void PrintList(SqList L)
{
	for(int i=0;i<L.length;i++)
		printf("%d ", L.data[i]);
	printf("\n");
}

//插入
bool ListInsert(SqList& L, int i, ElemType e)
{
	if (i<1 || i>L.length + 1) return false;//插入地址不合法
	if (L.length == MaxSize) return false;//表满

	//将第i个元素及之后的元素后移一个位置
	for (int j = L.length; j >= i; j--)
		L.data[j] = L.data[j - 1];

	L.data[i - 1] = e;
	L.length++;
	return true;
}

//删除
bool ListDelete(SqList& L, int i, ElemType& e)
{
	if (i<1 || i>L.length) return false;//删除地址不合法
	e = L.data[i - 1];
	for(int j =i;j<L.length;j++)
		L.data[j - 1] = L.data[j];
	L.length--;
	return true;
}

//查找
int ListFind(SqList L, ElemType e)
{
	for(int i=0;i<L.length;i++)
		if (L.data[i] == e) return i + 1;
	return 0;//未找到
}

//修改
bool ListModify(SqList& L, int i, ElemType e)
{
	if (i<1 || i>L.length) return false;//修改地址不合法
	L.data[i - 1] = e;
	return true;
}

//销毁
void DestroyList(SqList& L)
{
	L.length = 0;
}

测试文件

cpp 复制代码
test.c
#include"LList.h"

int main()
{
//创建顺序表+初始化
	SqList L;
	InitList(L);
//插入元素
	ListInsert(L, 1, 1);
	ListInsert(L, 2, 2);
	ListInsert(L, 3, 3);
	ListInsert(L, 4, 10);
//打印已有元素
	PrintList(L);
//删除对应下标的元素并得到元素值
	ElemType e;
	ListDelete(L, 4, e);
	PrintList(L);
	printf("%d\n", e);
//查找下标对应的元素
	printf("%d\n",ListFind(L, 3));
//修改对应下标元素的值
	ListModify(L, 2, 222);
	PrintList(L);
//销毁顺序表,将表长定义为0
	DestroyList(L);
	PrintList(L);

	return 0;
}

动态顺序表

由于静态顺序表依据数组构建,初始长度已经给定不可扩容,因此引入指针动态开辟内存即用即扩,使得顺序表更加灵活,空间使用率更高。

**malloc(申请空间的字节大小):**再内存中申请一块连续的空间,并返回空间的首地址,因此在接受的时候需要强转为对应数据类型的指针。

头文件:

cpp 复制代码
DLList.h
#pragma once
#include<iostream>
#include<algorithm>

using namespace std;

#define ElemType int
#define InitSize 10 //初始化表长
#define IncreaseSize 5 //增量

typedef struct DLNode {
	ElemType* data;
	int MaxSize;
	int length;
}DSList;

//初始化
void InitList_DL(DSList& L);

//增长
void IncreaseList_DL(DSList& L);

//销毁
void DestroyList_DL(DSList& L);

//打印
void PrintList_DL(DSList L);

//插入
bool ListInsert_DL(DSList& L, int i, ElemType e);

//删除
bool ListDelete_DL(DSList& L, int i, ElemType& e);

//按值查找
int LocateElem_DL(DSList L, ElemType e);

实现文件:

cpp 复制代码
DLList.cpp
#include"DLList.h"

#include"DLList.h"

//初始化
void InitList_DL(DSList& L) {
	L.data = (ElemType*)malloc(InitSize * sizeof(ElemType));
	L.length = 0;
	L.MaxSize = InitSize;
}

//增长
//malloc重新分配内存空间,并将原来数据拷贝到新空间,释放原来空间
void IncreaseList_DL(DSList& L)
{
	ElemType* p = L.data;
	L.data = (ElemType*)malloc((L.MaxSize + IncreaseSize) * sizeof(ElemType));
	for (int i = 0; i < L.length; i++)
		L.data[i] = p[i];
	free(p);
	L.MaxSize += IncreaseSize;
}

//销毁
void DestroyList_DL(DSList& L)
{
	free(L.data);
	L.data = nullptr;//防止出现野指针可将data置为空
	L.length = 0;
	L.MaxSize = 0;
}

//打印
void PrintList_DL(DSList L)
{
	for(int i=0;i<L.length;i++)
		printf("%d ", L.data[i]);
	printf("\n");
}

//插入
bool ListInsert_DL(DSList& L, int i, ElemType e)
{
	if (i<1 || i>L.length + 1) return false;
	if (L.length == L.MaxSize)
		IncreaseList_DL(L);
	for (int j = L.length; j >= i; j--)
		L.data[j] = L.data[j - 1];
	L.data[i - 1] = e;
	L.length++;
}

//删除
bool ListDelete_DL(DSList& L, int i, ElemType& e)
{
	if (i<1 || i>L.length) return false;	
	e = L.data[i - 1];
	
	for(int j = i;j<L.length;j++)
		L.data[j - 1] = L.data[j];
	L.length--;
	return true;
}

//按值查找
int LocateElem_DL(DSList L, ElemType e)
{
	for(int i=0;i<L.length;i++)
	{
		if (L.data[i] == e)
			return i + 1;
	}
	return 0;//找不到返回0
}

测试文件

cpp 复制代码
test.cpp
#include"DLList.h"

int main()
{
	DSList L;
	InitList_DL(L);
	for (int i = 1; i <= 15; i++)
		ListInsert_DL(L, i, i * 10);
	PrintList_DL(L);
	ElemType e;
	ListDelete_DL(L, 5, e);
	PrintList_DL(L);
	DestroyList_DL(L);
	PrintList_DL(L);

	return 0;
}
相关推荐
颜酱7 小时前
图的数据结构:从「多叉树」到存储与遍历
javascript·后端·算法
saltymilk12 小时前
使用 C++ 模拟 ShaderLanguage 的 swizzle
c++·模板元编程
zone773912 小时前
006:RAG 入门-面试官问你,RAG 为什么要切块?
后端·算法·面试
CoovallyAIHub15 小时前
OpenClaw 近 2000 个 Skills,为什么没有一个好用的视觉检测工具?
深度学习·算法·计算机视觉
CoovallyAIHub15 小时前
CVPR 2026 | 用一句话告诉 AI 分割什么——MedCLIPSeg 让医学图像分割不再需要海量标注
深度学习·算法·计算机视觉
CoovallyAIHub15 小时前
Claude Code 突然变成了 66 个专家?这个 5.8k Star 的开源项目,让我重新理解了什么叫"会用 AI"
深度学习·算法·计算机视觉
兆子龙15 小时前
前端哨兵模式(Sentinel Pattern):优雅实现无限滚动加载
前端·javascript·算法
xlp666hub18 小时前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
CoovallyAIHub19 小时前
9个视觉语言模型工厂实测:Qwen 87.9%碾压全场,你的显卡能跑哪个?
算法
SparkX开源AI知识库19 小时前
手摸手带你安装OpenClaw并对接飞书
算法·架构