本文共 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/