数据结构算法实现-顺序表实现

C++实现数据结构代码,本文参考部分博主代码,仅用于学习


顺序表简单实现

代码如下(示例):

关于数据结构实现与实践可以参考
数据结构的实践应用

/*
Project: sequence_list(数据结构-顺序表)
Date:    2018/09/12  20191012修改 添加Reverse  20200819修改 添加ClearList
Author:  Frank Yu
CreateList(SqList &L,int n) 参数:顺序表L,顺序表长度n 功能:创建长度为的顺序表 时间复杂度:O(n)
InitList(SqList &L) 参数:顺序表L 功能:初始化 时间复杂度:O(1)
InsertList(SqList &L,int i,ElemType e) 参数:顺序表L,位置i,元素e 功能:位置i处插入元素e 时间复杂度:O(n)
ListDelete(SqList &L,int i) 参数:顺序表L,位置i 功能:删除位置i处元素 时间复杂度:O(n)
LocateElem(SqList L,ElemType e) 参数:顺序表L,元素e 功能:返回第一个等于e的元素的位置 时间复杂度:O(n)
Reverse(SqList &L) 参数:顺序表L 倒置函数 将原顺序表直接倒置
PrintList(SqList L) 参数:顺序表L 功能:遍历L,并输出
SplitSort(SqList &L) 参数:顺序表L 功能:分开奇偶,并分开排序
ClearList(SqList &L) 参数:顺序表L 功能:清空顺序表
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<iostream>
#define MaxSize 100
#define ElemType int
#define Status int
using namespace std;
//顺序表数据结构
typedef struct {
	ElemType data[MaxSize];
	int length;
}SqList;

//初始化顺序表,构造一个空的顺序表
Status InitList(SqList &L) {
	memset(L.data, 0, sizeof(L));
	L.length = 0;
	return 0;
}

//创建顺序表函数,初始化前m个数据
bool CreatList(SqList &L, int m) {
	if (m<0 || m>MaxSize)
		return false;
	for (int i = 0; i < m; i++) {
		cin >> L.data[i];
		L.length++;
	}
	return true;
}
//插入函数,在位置i开始插入数据e    1<=i<=length+1
bool InsertList(SqList &L, int i,ElemType e) {
	if (i<1 || i>L.length+1) {
		cout << "插入位置无效" << endl;
		return false;
	}
	if (L.length >= MaxSize) {
		cout << "当前存储空间已满" << endl;
		return false;
	}
	for (int j = L.length; j >= i; j--) {
		L.data[j] = L.data[j-1];
	}
	L.data[i-1] = e;
	L.length++;
	return true;
}
//删除函数,删除第i个的元素,其余元素依次移动 1<=i<=L.length+1
bool ListDelete(SqList &L, int i) {
	if (i<1 || i>L.length) {
		cout << "位置无效" << endl;
		return false;
	}
	for (int j = i; j < L.length - 1; j++) {
		L.data[j - 1] = L.data[j];
	}
	L.length--;
	return true;
}
//查找函数 按位置从小到大查找第一个值等于e的元素 并返回位置
int LocateElem(SqList L, ElemType e) {
	int i = 0;
	while (i < L.length) {
		if (L.data[i] == e) {
			return i+1;
		}
		i++;
	}
	return 0;
}
//倒置函数 将原顺序表直接倒置
void Reverse(SqList &L) {
	if (L.length) {
		for (int i = 0; i < L.length - 1 - i; i++) {
			int t = L.data[i];
			L.data[i] = L.data[L.length - 1 - i];
			L.data[L.length - 1 - i] = t;
		}
	}
}
//对顺序表进行排序 冒泡排序实现
void ListSort(SqList &L) {
	if (L.length) {
		int temp,flag;
		for (int i = 0; i < L.length - 1; i++) {
			flag = 0;
			for (int j = 0; j < L.length -1- i; j++) {
				if (L.data[j + 1] < L.data[j]) {
					temp = L.data[j + 1];
					L.data[j + 1] = L.data[j];
					L.data[j] = temp;
					flag = 1;
				}
			}
			if (flag == 0)
				break;
		}
	}
}
//清空顺序表
void ClearList(SqList &L) {
	L.length = 0;
}

//输出当前顺序表的元素
void CoutList(SqList L) {
	cout << "当前顺序表的元素为:" << endl;
	int len = 0;
	while (len < L.length) {
		cout << setw(4) << L.data[len];
		len++;
	}
	cout << endl;
}
//创建顺序表函数
void Creat(SqList &L) {
	int n;
	bool flag;
	cout << "输入要创建的顺序表长度: ";
	cin >> n;
	cout << endl;
	cout << "输入顺序表元素: ";
	flag = CreatList(L, n);
	if (flag) {
		cout << "创建成功!" << endl;
		CoutList(L);
	}
	else
		cout << "输入长度非法,创建失败" << endl;
	
}
//插入功能函数 调用InsertList完成顺序表元素插入 调用PrintList函数显示插入成功后的结果
void Insert(SqList &L)
{
	int place; 
	ElemType e; 
	bool flag;
	cout << "请输入要插入的位置(从1开始)及元素:\n";
	cin >> place >> e;
	flag = InsertList(L, place, e);
	if (flag)
	{
		cout << "插入成功!";
		CoutList(L);
	}
}
//删除功能函数 调用ListDelete函数完成顺序表的删除 调用PrintList函数显示插入成功后的结果
void Delete(SqList &L)
{
	int place; bool flag;
	cout << "请输入要删除的位置(从1开始):\n";
	cin >> place;
	flag = ListDelete(L, place);
	if (flag)
	{
		cout << "删除成功!!!\n";
		CoutList(L);
	}
}
//查找功能函数 调用LocateElem查找元素
void Search(SqList L)
{
	ElemType e; int flag;
	cout << "请输入要查找的值:\n";
	cin >> e;
	flag = LocateElem(L, e);
	if (flag)
	{
		cout << "该元素位置为:" << flag << endl;
	}
	else
		cout << "未找到该元素!\n";
}
void menu() {
	cout << setw(6) << "1.创建" << setw(6) << "2.插入" << endl;
	cout << setw(6) << "3.删除" << setw(6) << "4.查找" << endl;
	cout << setw(6) << "5.倒置" << setw(6) << "6.排序" << endl;
	cout << setw(6) << "7.清空" << setw(6) << "8.退出" << endl;
	cout << setw(6) << "9.输出" << endl;
}
int main() {
	SqList L;
	int choice;
	InitList(L);
	while (1) {
		menu();
		cout << "输入选项:";
		cin >> choice;
		if (choice == 8)
			break;
		switch (choice)
		{
			case 1:Creat(L); break;
			case 2:Insert(L); break;
			case 3:Delete(L); break;
			case 4:Search(L); break;
			case 5:Reverse(L); break;
			case 6:ListSort(L); break;
			case 7:ClearList(L); break;
			case 9:CoutList(L); break;
			default:cout << "输入错误!" << endl;
		}
	}
	return 0;
}

更多内容请访问:https://juzihhu.github.io/

  • 6
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值