顺序表基本 *** 作(CC++)

顺序表基本 *** 作(CC++),第1张

前言

顺序表的基本 *** 作:定义、初始化、插入、删除、按值查找、按位查找、打印顺序表、打印表长


一、代码
#include
#include
#define Initsize 10
using namespace std;

//定义顺序表
typedef struct {
	int* data;
	int length;
	int Maxsize;
}SeqList;

//初始化顺序表
void InitList(SeqList& L) {
	L.data = (int*)malloc(Initsize * sizeof(int));
	for (int i = 0; i < Initsize; i++)
	{
		L.data[i] = 0;
	}
	L.length = 0;
	L.Maxsize = Initsize;
	cout << "初始化成功..." << endl;
}

//插入数据
void ListInsert(SeqList& L, int i, int e) {
	if (L.length >= L.Maxsize) {
		cout << "插入失败,表已满..." << endl;
		return;
	}
	if (i > L.length + 1 || i < 1)
	{
		cout << "插入位置不合法..." << endl;
		return;
	}
	for (int j = L.length; j >= i; j--)
	{
		L.data[j] = L.data[j - 1];
	}
	L.data[i - 1] = e;
	L.length++;
	cout << "插入成功..." << endl;

}

//删除数据
int ListDelete(SeqList& L, int i) {
	int e=9999;
	if (L.length <= 0) {
		cout << "删除失败,表是空的..." << endl;
	}
	else if (i > L.length + 1 || i < 1)
	{
		cout << "删除位置不合法..." << endl;
	}
	else {
		e = L.data[i - 1];
		for (int j = i; j < L.length; j++)
		{
			L.data[j] = L.data[j + 1];
		}
		L.data[L.length - 1] = 0;
		L.length--;
		cout << "删除成功..." << endl;
	}
	return e;

}

//按值查找
int LocateElem(SeqList L, int e) {
	int i = 9999;
	for (int j = 0; j < L.length; j++)
	{
		if (e==L.data[j])
		{
			i = j;
			break;
		}
	}

	return i;
}

//按位查找
int GetElem(SeqList L, int i) {
	int e = 9999;
	if (L.length <= 0) {
		cout << "查询失败,表是空的..." << endl;
	}
	else if (i > L.length + 1 || i < 1)
	{
		cout << "查找位置不合法..." << endl;
	}
	else {
		e = L.data[i - 1];
	}
	return e;
}

//打印顺序表
void PrintList(SeqList L) {
	cout << "当前顺序表:" << endl;
	for (int i = 0; i < L.length; i++)
	{
		cout << "L.data[" << i << "] = " << L.data[i] << endl;
	}
}

//打印顺序表长度
void Length(SeqList L) {
	cout << "当前顺序表长度: " << L.length << endl;
}

//菜单
void menu(SeqList &L) {
	int n, e, i;
	do
	{
		cout << "\t\t顺序表 *** 作菜单\t\t" << endl;
		cout << "\t\t1.初始化\t\t" << endl;
		cout << "\t\t2.插入数值\t\t" << endl;
		cout << "\t\t3.打印表\t\t" << endl;
		cout << "\t\t4.打印表长度\t\t" << endl;
		cout << "\t\t5.删除元素\t\t" << endl;
		cout << "\t\t6.按值查找元素\t\t" << endl;
		cout << "\t\t7.按位查找元素\t\t" << endl;
		cout << "\t\t8.退出\t\t" << endl;
		cout << "请输入序号:" << endl;
		cin >> n;
		switch (n)
		{
		case 1:InitList(L);//初始化顺序表
			break;
		case 2:
			cout << "请输入你要插入的位置和数值" << endl;
			cin >> i >> e;
			ListInsert(L, i, e);
			break;
		case 3:PrintList(L);
			break;
		case 4:Length(L);
			break;
		case 5:
			cout << "请输入你要删除的位置" << endl;
			cin >> i;
			e = ListDelete(L, i);
			if (e == 9999) {
				break;
			}
			cout << "已删除 L.data[" << i - 1 << "] = " << e << endl;
			break;
		case 6:
			cout << "请输入你要查找的数值" << endl;
			cin >> e;
			i = LocateElem(L, e);
			if (i != 9999) {
				cout << "已查出 L.data[" << i << "] = " << e << endl;
			}
			else {
				cout << "未找到..." << endl;
			}
			break;
		case 7:
			cout << "请输入你要查找的位置" << endl;
			cin >> i;
			e = GetElem(L, i);
			if (e != 9999) {
				cout << "已查出 L.data[" << i-1 << "] = " << e << endl;
			}
			else {
				cout << "未找到..." << endl;
			}
			break;
		case 8: return ;
		default:
			cout << "输入错误" << endl;
			break;
		}

	} while (true);
}

int main() {
	SeqList L;//创建数据表
	menu(L);
	cout << "程序已结束运行...";
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/790727.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存