博客
关于我
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/

你可能感兴趣的文章
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>