博客
关于我
C++ struct实现顺序表
阅读量:483 次
发布时间:2019-03-06

本文共 3745 字,大约阅读时间需要 12 分钟。

#include 
#include
using namespace std;#define MAXSIZE 1000typedef int ElemType;typedef struct { ElemType elem[MAXSIZE] ; int length;} SqList;void InitList(SqList &L) { //初始化 memset(L.elem, 0, sizeof(L.elem)); L.length = 0;}bool ClearList(SqList &L) { //清空顺序表 L.length = 0; return true;}int GetLength(SqList L) { //得到长度 return (L.length);}bool IsEmpty(SqList L) { //判断是否为空 if (L.length == 0) return true; else return false;}bool GetElem(SqList L, int i, ElemType &e) { //查询某位置的元素 if (i < 1 || i > L.length) return false; e = L.elem[i - 1]; return true;}int LocateElem_one(SqList L, ElemType e) { //查询某元素的位置 for (int i = 0; i < L.length; i++) if (L.elem[i] == e) return i + 1; return 0;}int LocateElem_two(SqList L, ElemType e) { //查询某元素的位置 int i = 0; while (i < L.length && L.elem[i] != e) i++; if (i < L.length) return i + 1; else return 0;}bool ListInsert(SqList &L, int i, ElemType e) { //插入元素 if ( i < 1 || i > L.length + 1) return false; if (L.length == MAXSIZE) return false; for (int j = L.length - 1 ; j >= i - 1 ; j--) { L.elem[j + 1] = L.elem[j]; } L.elem[i - 1] = e; L.length++; return true;}bool ListDelete(SqList &L, int i) { //删除元素 if (i < 1 || i > L.length) return false; for (int j = i; j <= L.length - 1; j++) { L.elem[j - 1] = L.elem[j]; } L.length--; return true;}bool CreateList(SqList &L, int n) { //创建顺序表 for (int i = 0; i < n; i++) { cout << "第" << i + 1 << "个元素是:"; cin >> L.elem[i]; L.length++; } return true;}void PrintList(SqList L) { //打印 for (int i = 0; i < L.length; i++) { cout << "第" << i + 1 << "个元素是:" << L.elem[i] << endl; }}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 << "7--------------------清空顺序表-----------------------" << endl; cout << "8--------------------输入顺序表元素--------------------" << endl; cout << "9--------------------退出------------------------------" << endl; cout << "------------------------------------------------------" << endl;}int main() { SqList L;//创建顺序表 InitList(L);//初始化 int cnt; while (1) { menu();//菜单 cin >> cnt; system("cls");//清空屏幕 if (cnt == 1) { cout << "请输入元素的个数,然后依次输入元素" << endl; int n; cin >> n; if (CreateList(L, n)) { cout << "创建成功" << endl; } } else if (cnt == 2) cout << "当前顺序表长度为:" << GetLength(L) << endl; else if (cnt == 3) { cout << "请依次输入要插入的位置和元素" << endl; int pos; ElemType elem; cin >> pos >> elem; if (ListInsert(L, pos, elem)) { cout << "插入成功" << endl; } } else if (cnt == 4) { cout << "请输入要删除的元素位置" << endl; int pos; cin >> pos; if (ListDelete(L, pos)) { cout << "删除成功" << endl; } } else if (cnt == 5) { cout << "请输入要查询位置的元素" << endl; ElemType elem; cin >> elem; int pos = LocateElem_one(L, elem); if (pos == 0) { cout << "查询不到" << endl; } else cout << "该元素的位置是" << pos << endl; } else if (cnt == 6) { ElemType elem; int pos; cout << "输入要查询的位置" << endl; cin >> pos; if (GetElem(L, pos, elem)) { cout << "该位置的元素为:" << elem << endl; } else cout << "输入有误" << endl; } else if (cnt == 7) { if (ClearList(L)) cout << "顺序表已清空" << endl; } else if (cnt == 8) { PrintList(L); } else if (cnt == 9) { return 0; } } return 0;}

转载地址:http://iyhdz.baihongyu.com/

你可能感兴趣的文章
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>