C++如何实现数据结构的顺序表
发表于:2025-02-09 作者:千家信息网编辑
千家信息网最后更新 2025年02月09日,这篇文章给大家分享的是有关C++如何实现数据结构的顺序表的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。代码1.SeqList.h#ifndef SEQLIST_H#defi
千家信息网最后更新 2025年02月09日C++如何实现数据结构的顺序表
这篇文章给大家分享的是有关C++如何实现数据结构的顺序表的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
代码
1.SeqList.h
#ifndef SEQLIST_H#define SEQLIST_H#includeusing namespace std;template class SeqList{ T data[MAXSIZE]; int length;public: SeqList(); SeqList(T a[],int n); ~SeqList(); int ListLength(); T Get(int pos); int Locate(T item); void SeqPrint(); void Insert(int i, T item); T Delete(int i);};#endif
2.SeqList.cpp
#define _CRT_SECURE_NO_WARNINGS 1#include"SeqList.h"templateSeqList ::SeqList(){ length = 0;}template SeqList ::SeqList(T a[], int n){ if (n < MAXSIZE) { length = n; for (int i = 0; i < n; i++) { data[i] = a[i]; } } else { cerr << "您的数据已经超过范围,系统无法继续工作" << endl; exit(-1); }}template SeqList ::~SeqList(){}template int SeqList ::ListLength(){ return length;}template T SeqList ::Get(int pos){ if (pos > length || pos < 0) { cerr << "您要查找的位置不存在,系统无法继续为您服务" << endl; exit(-1); } else { return data[pos - 1]; }}template int SeqList ::Locate(T item){ for (int i = 0; i < length; i++) { if (data[i] == item) return i + 1; } return -1;}template void SeqList ::SeqPrint(){ for (int i = 0; i < length; i++) { cout << data[i] << " "; } cout << endl;}template void SeqList ::Insert(int i, T item){ if (length < MAXSIZE) { for (int j = length - 1; j>=i - 1; j--) { data[j + 1] = data[j]; } data[i - 1] = item; length++; } else { cerr << "抱歉,当前已经达到系统最大的储存,无法为您插入" << endl; exit(-1); }}template T SeqList ::Delete(int i){ if (length == 0) { cerr << "当前无可删除元素" << endl; exit(-1); } if (i<1 || i>length) { cerr << "该位置非法" << endl; exit(-1); } T x = data[i - 1]; for (int j = i; j < length; j++) { data[j - 1] = data[j]; } length--; return x;}
3.test.cpp
#define _CRT_SECURE_NO_WARNINGS 1#include"SeqList.cpp"#includeusing namespace std;void menu(){ cout << "|------------------------------------|" << endl; cout << "|----------- 欢迎来到顺序表 ---------|" << endl; cout << "|---------------1.插入---------------|" << endl; cout << "|---------------2.删除---------------|" << endl; cout << "|---------------3.求长---------------|" << endl; cout << "|---------------4.取值---------------|" << endl; cout << "|---------------5.定位---------------|" << endl; cout << "|---------------6.打印---------------|" << endl; cout << "|---------------0.退出---------------|" << endl; cout << "|------------------------------------|" << endl;}int main(){ int *a; int n; cout << "请输入您要构造的顺序表的长度" << endl; cin >> n; a = new int[n]; cout << "请输入该顺序表中的每一个元素" << endl; for (int i = 0; i < n; i++) { cin >> a[i]; } SeqList seq(a, n); cout << "现在开始我们的程序之旅" << endl; int input=0; do { menu(); cout << "输入您要进行的操作的编号" << endl; cin >> input; switch (input) { case 1: cout << "请输入您要插入的位置和数值" << endl; int pos; int value; cin >> pos; cin >> value; seq.Insert(pos,value); break; case 2: cout << "请输入您要删除的位置" << endl; int pos1; cin >> pos1; cout << "您删除的元素的值为:"; cout << seq.Delete(pos1) << endl; break; case 3: cout << "您的顺序表当前的长度为:" << seq.ListLength() << endl; break; case 4: cout << "请输入您要查找的位置" << endl; int pos2; cin >> pos2; cout << "您查找的元素的值为:"; cout << seq.Get(pos2) << endl;; break; case 5: cout << "请输入您要查找的元素" << endl; int item; cin >> item; cout << "您查找的元素的位置为:"; cout << seq.Locate(item) << endl;; break; case 6: cout << "当前顺序表如下:" << endl; seq.SeqPrint(); break; case 0: cout << "程序退出,感谢使用" << endl; exit(-1); break; default : cout << "您的输入有误,请重新选择" << endl; } } while (input); return 0;}
感谢各位的阅读!关于"C++如何实现数据结构的顺序表"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
输入
顺序
位置
元素
数据
系统
数据结构
结构
C++
内容
更多
程序
篇文章
长度
不错
实用
最大
抱歉
为您服务
之旅
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
青岛联信网络技术怎么样
数据库技术沙龙
杭州晨晞网络技术有限公司
容错服务器功率
mc怎么连服务器
excel数据太多数据库
PS软件开发思维
系统软件开发企业
联盟手游服务器在哪个地方
原神为什么要开两个服务器
内疚伴奏软件开发
阿里云mysql数据库ip
小学生网络安全公约文字
出现网络安全问题怎么办
福建惠普服务器虚拟化系统物理机
惠州惠阳软件开发
金典网络技术
我的世界服务器字
核心交换机连接服务器怎么连
java怎么看数据库中视图
北京飞书网络技术有限公司
默纳克服务器有密码怎么办
容错服务器功率
肇庆量化交易app软件开发推荐
天津租赁gpu服务器虚拟主机
服务器内网穿透转发
无线网络安全规范
燃烧之刃服务器多少钱
上海联想服务器续保安装
哪里学习网络安全