顺序表的原理及实现


前言


提示:以下是本篇文章正文内容,下面案例可供参考

一、顺序表是什么?

	线性表的顺序存储又称顺序表。
 它是用一组地址连续的存储单元依次存储线性表中的数据元素,从而使得逻辑上相邻的两个元素在物理地址上也相邻。
	顺序表的特点是表中元素的逻辑顺序与其物理顺序相同。
	线性表的顺序存储结构是一种随机存取的存储结构。
	顺序表的存储密度高,每个结点只存储数据元素。

二、创建顺序表

代码如下(示例):

typedef struct{
	int* elems;   //顺序表基地址
	int length;  //顺序表长度
	int size;   //顺序表总空间大小
}sqlist;//顺序表的类型名

在main函数中创建list顺序表:

sqlist list;

三、顺序表的初始化及销毁

1.顺序表的初始化

首先需要书写初始化函数 initList:
代码如下(示例):

bool initList(sqlist& L) {
	L.elems = new int[MAX_SIZE];  //为顺序表分配MAX_SIZE个int元素的空间
	if (!L.elems) return false;   //判断首地址是否存在
	L.length = 0;				//顺序表的初始长度为0
	L.size = MAX_SIZE;			//顺序表的最大存储空间
	return true;
}

然后在主函数中调用函数initList:

if (initList(list)) {
		cout << "顺序表初始化成功!" << endl;
	}
	else cout << "顺序表初始化失败!" << endl;

	listPrint(list);

2.顺序表的销毁

书写listDestroy函数
代码如下(示例):

void listDestroy(sqlist &L) {
	if (L.elems) delete[] L.elems;
	L.length = 0;
	L.size = 0;
}

主函数中调用该函数

listDestroy(list);

四、顺序表的基本操作

1.顺序表的添加元素

bool listappend(sqlist& L, int e) {
	if (L.length == L.size) return false;	//判读顺序表中元素个数是否到达最大值
	L.elems[L.length] = e;
	L.length++;								//属性表长度加一
	return true;

}

2.顺序表的插入元素

bool listInsert(sqlist& L, int index, int e) {
	if (L.length == L.size) return false;
	if (index<0 || index>=L.length) return false;   //判断输入的位置是否正确
	for (int j = L.length - 1; j >= index; j--) {
		L.elems[j + 1] = L.elems[j];
		}
	L.elems[index] = e;
	L.length++;
	return true;
}

3.顺序表的删除元素

bool listDelete(sqlist &L,int index) {
	if (index < 0 || index >= L.length) return false;  //判断输入的位置是否正确
	if (index == L.length - 1) {
		L.length--; return true;
	}
	for (int j = index; j < L.length-1; j++) {
		L.elems[j] = L.elems[j + 1];
	}
	L.length--;
	return true;
}

4.打印顺序表的元素

void listPrint(sqlist& L) {
	cout<<endl << "顺序表存储空间size: " << L.size << ",已保存的个数: " << L.length << endl;
	for (int i = 0; i < L.length; i++) {
		cout << L.elems[i] << " ";
	}
	cout << endl;

}

五、完整代码

vs2019(c++17)

#include<iostream>
using namespace std;
#define MAX_SIZE 100


typedef struct{
	int* elems;   //顺序表基地址
	int length;  //顺序表长度
	int size;   //顺序表总空间大小
}sqlist;


bool initList(sqlist& L) {
	L.elems = new int[MAX_SIZE];  //为顺序表分配MAX_SIZE个int元素的空间
	if (!L.elems) return false;
	L.length = 0;
	L.size = MAX_SIZE;
	return true;
}


void listPrint(sqlist& L) {
	cout<<endl << "顺序表存储空间size: " << L.size << ",已保存的个数: " << L.length << endl;
	for (int i = 0; i < L.length; i++) {
		cout << L.elems[i] << " ";
	}
	cout << endl;

}


bool listappend(sqlist& L, int e) {
	if (L.length == L.size) return false;	
	L.elems[L.length] = e;
	L.length++;
	return true;

}


bool listInsert(sqlist& L, int index, int e) {
	if (L.length == L.size) return false;
	if (index<0 || index>=L.length) return false;
	for (int j = L.length - 1; j >= index; j--) {
		L.elems[j + 1] = L.elems[j];
		}
	L.elems[index] = e;
	L.length++;
	return true;
}


bool listDelete(sqlist &L,int index) {
	if (index < 0 || index >= L.length) return false;
	if (index == L.length - 1) {
		L.length--; return true;
	}
	for (int j = index; j < L.length-1; j++) {
		L.elems[j] = L.elems[j + 1];
	}
	L.length--;
	return true;
}


void listDestroy(sqlist &L) {
	if (L.elems) delete[] L.elems;
	L.length = 0;
	L.size = 0;
}


int main() {
	sqlist list;
	int e,index;

	cout << "顺序表初始化..." << endl;

	//1.初始化
	if (initList(list)) {
		cout << "顺序表初始化成功!" << endl;
	}
	else cout << "顺序表初始化失败!" << endl;

	listPrint(list);

	//2.添加元素
	int count = 0;
	cout << "请输入添加元素的个数:";
	cin >> count;

	for (int i = 0; i < count; i++) {
		cout << "请输入添加的元素e的值:";
		cin >> e;
		if (listappend(list, e)) {
			cout << "添加成功!" << endl;
		}
		else {
			cout << "添加失败!" << endl;
		}
	}

	listPrint(list);

	//3.插入元素
	cout << "请输入要插入的元素位置及值" << endl;
	cin >> index >> e;

	if (listInsert(list, index, e)) {
		cout << "插入成功!" << endl;
	}
	else {
		cout << "插入失败!" << endl;
	}

	listPrint(list);

	//4.删除元素
	cout << "请输入要删除的元素的位置" << endl;
	cin >> index;

	if (listDelete(list, index)) {
		cout << "删除成功!" << endl;
	}
	else {
		cout << "删除失败!" << endl;
	}

	listPrint(list);

	//5.销毁
	listDestroy(list);

	system("pause");
	return 0;
}

总结

例如:以上就是今天要讲的内容,本文仅仅简单介绍了顺序表的创建及使用,希望大家点点关注。
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐