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

静态顺序表

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

参考代码

头文件:

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

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;
}
相关推荐
2401_8920709820 小时前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
yuzhuanhei20 小时前
Visual Studio 配置C++opencv
c++·学习·visual studio
小O的算法实验室20 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
‎ദ്ദിᵔ.˛.ᵔ₎21 小时前
LIST 的相关知识
数据结构·list
不爱吃炸鸡柳21 小时前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
M--Y21 小时前
Redis常用数据类型
数据结构·数据库·redis
十五年专注C++开发21 小时前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
‎ദ്ദിᵔ.˛.ᵔ₎21 小时前
STL 栈 队列
开发语言·c++
2401_8920709821 小时前
【Linux C++ 日志系统实战】高性能文件写入 AppendFile 核心方法解析
linux·c++·日志系统·文件写对象
郭涤生21 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法