C++顺序表操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++顺序表操作

我不是秃头sheep   2022-10-20 我要评论

编写程序,实现顺序表的下列功能:

  • 从键盘输入数据建立一个顺序表
  • 输出该顺序表
  • 往顺序表中插入数据
  • 从顺序表中删除数据
  • 给定数据,进行查找,给出查找成功和失败信息

C++代码:

#include <iostream>
#include <string> 
#include "windows.h"
using namespace std;

// 定义顺序表结构体
struct SequentialList
{
	int* base;   // 顺序表首地址
	int length;  // 顺序表长度
	int count;	 // 顺序表当前元素个数
};

// 初始化结构体
bool initSequentialList(SequentialList& s) {
	
	// 如果之前已有数组,则释放之前的数组
	if (s.length >= 0) {
		delete s.base;
	}

	// 初始化一个长度为size数组
	int size;
	cout << "输入顺序表的长度:";
	cin >> size;
	s.base = new int[size];
	s.length = size;
	s.count = 0;
	// 清理屏幕
	system("cls||clear");
	// 判断初始化成功或失败
	if (s.base != NULL) {
		return true;
	}else {
		return false;
	}
}


// 打印顺序表各个元素
void printItems(SequentialList& s) {
	// 清理屏幕
	system("cls||clear");
	if (s.count > 0) {
		string temp(10, '-');
		cout << temp + "打印顺序表" + temp << endl;
		for (int i = 0; i < s.count; i++) {
			cout << "[" + to_string(i) + "]" + to_string(s.base[i]) << endl;
		}
	}
	else {
		cout << "无元素打印!" << endl;
	}
}

// 头插
bool topInsert(SequentialList& s, int item) {
	// 清理屏幕
	system("cls||clear");
	// 表满 操作失败
	if (s.count == s.length) {
		return false;
	}
	if (s.count > 0) {
		// 所有元素向后移动1位
		for (int i = s.count - 1; i >= 0; i--) {
			s.base[i + 1] = s.base[i];
		}
	}
	// 如果count为0 正常插入元素
	s.base[0] = item;
	s.count ++;
	return true;
}


// 尾插
bool bottomInsert(SequentialList& s, int item) {
	// 清理屏幕
	system("cls||clear");
	if (s.count == s.length) { // 表满->结束
		return false;
	}else {	// 没满直接尾插
		s.base[s.count] = item;
		s.count++;
		return true;
	}
}

// 指定位置插入
bool indexInsert(SequentialList& s, int index, int item) {
	// 清理屏幕
	system("cls||clear");
	// 判断下标是否给错 或者 表满 -> 结束
	if (0 > index || index > s.count || s.length == s.count) {
		return false;
	}
	if (index == 0) {
		// 调用头插
		topInsert(s, item);
	}else if (index == s.count) {
		// 调用尾插
		bottomInsert(s, item);
	}else {
		// index以及后面的所有元素 向后移动1位
		for (int i = s.count - 1; i >= index; i--) {
			s.base[i + 1] = s.base[i];
		}
		//插入操作
		s.base[index] = item;
		s.count++;
	}
	return true;

}

// 头删
bool topDelete(SequentialList& s) {
	// 清理屏幕
	system("cls||clear");
	// 如果没元素 -> 结束
	if (s.count == 0) {
		return false;
	}
	// 元素个数大于1 所有元素向前移动1位
	if (s.count > 1) {
		for (int i = 0; i < s.count - 1; i++) {
			s.base[i] = s.base[i + 1];
		}
	}
	s.count--;
	return true;
}

// 尾删
bool bottomDelete(SequentialList& s) {
	// 清理屏幕
	system("cls||clear");
	// 如果没元素 -> 结束
	if (s.count == 0) {
		return false;
	}
	// 伪删除
	s.count--;
	return true;
}

// 删除指定位置的元素
bool indexDelete(SequentialList& s, int index) {
	// 清理屏幕
	system("cls||clear");
	// 没元素 或 给错下标 -> 结束
	if (s.count == 0 || index < 0 || index >= s.count ) {
		return false;
	}

	if (index == 0) {
		// 调用头删
		topDelete(s);
	}else if (index == s.count) {
		// 调用尾删
		bottomDelete(s);
	}else {
		// index后面的元素向前覆盖
		for (int i = index; i < s.count - 1; i++) {
			s.base[i] = s.base[i + 1];
		}
		s.count--;
	}
	return true;
}

// 查找某元素在顺序表的位置
void findElement(SequentialList& s, int item) {
	// 清理屏幕
	system("cls||clear");
	// -1为找不到
	int count = 0;
	// 匹配
	for (int i = 0; i < s.count; i++) {
		if (s.base[i] == item) {
			count++;
			cout << "找到第" + to_string(count) + "个"+to_string(item)+"的下标为["+to_string(i)+"]" << endl;
		}	
	}
	if (count == 0) {
		cout << "未找到" + to_string(item) + "元素!" << endl;
	}
}

// 打印菜单
void printMenu(){
	string temp(10, '-');
	cout << endl;
	cout << temp+"操作菜单"+temp << endl;
	cout << "[1]建立一个顺序表" << endl;
	cout << "[2]打印顺序表" << endl;
	cout << "[3]向顺序表[头部]插入新元素" << endl;
	cout << "[4]向顺序表[尾部]插入新元素" << endl;
	cout << "[5]向顺序[指定位置]部插入新元素" << endl;
	cout << "[6]删除顺序表[头部]的元素" << endl;
	cout << "[7]删除顺序表[尾部]的元素" << endl;
	cout << "[8]删除顺序表[指定位置]的元素" << endl;
	cout << "[9]查找某元素在顺序表的位置" << endl;
	cout << "[0]退出操作" << endl;
	cout << temp+temp+temp << endl;
}
// 函数主入口
int main() {
	int options;			// 选项
	int flag = true;		// while循环标记
	SequentialList s;		// 顺序表结构体变量
	int newItem;			// 新元素
	int index;				// 插入|删除元素的下标
	while (flag) {
		printMenu();
		cout << "请操作:" ;
		cin >> options;
		switch (options) {
			case 1:
				if (initSequentialList(s)) {
					cout << "\t初始化成功" << endl;
				}
				else {
					cout << "\t初始化失败" << endl;
				}
				break;
			case 2:
				printItems(s);
				break;
			case 3:
				cout << "新元素:";
				cin >> newItem;
				
				if (topInsert(s, newItem)) {
					cout << "头部成功插入("+ to_string(newItem)+")" << endl;
				}
				else {
					cout << "顺序表已满,头部插入操作失败!!!" << endl;
				}
				break;
			case 4:
				cout << "新元素:";
				cin >> newItem;

				if (bottomInsert(s, newItem)) {
					cout << "尾部成功插入(" + to_string(newItem) + ")!!!" << endl;
				}
				else {
					cout << "顺序表已满,尾部插入操作失败!!!" << endl;
				}
				break;
			case 5:
				cout << "新元素:";
				cin >> newItem;
				cout << "插入位置:";
				cin >> index;
				if (indexInsert(s, index, newItem)) {
					cout << "在["+to_string(index) + "]成功插入(" + to_string(newItem) + ")!!!" << endl;
				}
				else {
					cout << "插入位置错误或顺序表已满,操作失败!!!" << endl;
				}
				break;
			case 6:
				if (topDelete(s)) {
					cout << "头部元素删除成功!!!" << endl;
				}
				else {
					cout << "头部元素删除操作失败!!!" << endl;
				}
				break;
			case 7:
				if (bottomDelete(s)) {
					cout << "尾部元素删除成功!!!" << endl;
				}
				else {
					cout << "尾部元素删除操作失败!!!" << endl;
				}
				break;
			case 8:
				cout << "删除位置:";
				cin >> index;
				if (indexDelete(s, index)) {
					cout <<"删除[" + to_string(index) + "]元素成功!!!" << endl;
				}
				else {
					cout << "删除位置错误或顺序表为空,操作失败!!!" << endl;
				}
				break;
			case 9:
				cout << "要查找的元素:";
				cin >> newItem;
				findElement(s, newItem);
				break;
			case 0:
				// 清理屏幕
				system("cls||clear");
				flag = false;
				cout << "---本次操作结束---" << endl;
				break;
			default:
				cout << "请输入正确的序号!!!" << endl;
				break;
		}
	}
	return 0;
}

运行结果:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们