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

静态顺序表

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

参考代码

头文件:

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

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;
}
相关推荐
wregjru2 小时前
【QT】4.QWidget控件(2)
c++
浅念-2 小时前
C++入门(2)
开发语言·c++·经验分享·笔记·学习
小羊不会打字2 小时前
CANN 生态中的跨框架兼容桥梁:`onnx-adapter` 项目实现无缝模型迁移
c++·深度学习
团子的二进制世界2 小时前
G1垃圾收集器是如何工作的?
java·jvm·算法
Max_uuc2 小时前
【C++ 硬核】打破嵌入式 STL 禁忌:利用 std::pmr 在“栈”上运行 std::vector
开发语言·jvm·c++
吃杠碰小鸡2 小时前
高中数学-数列-导数证明
前端·数学·算法
故事不长丨2 小时前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
long3162 小时前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
近津薪荼2 小时前
dfs专题4——二叉树的深搜(验证二叉搜索树)
c++·学习·算法·深度优先