C++ struct实现顺序表
生活随笔
收集整理的這篇文章主要介紹了
C++ struct实现顺序表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
#include <cstring>
using namespace std;
#define MAXSIZE 1000
typedef 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;elsereturn 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;elsereturn 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;} elsecout << "該元素的位置是" << pos << endl;} else if (cnt == 6) {ElemType elem;int pos;cout << "輸入要查詢的位置" << endl;cin >> pos;if (GetElem(L, pos, elem)) {cout << "該位置的元素為:" << elem << endl;} elsecout << "輸入有誤" << endl;} else if (cnt == 7) {if (ClearList(L))cout << "順序表已清空" << endl;} else if (cnt == 8) {PrintList(L);} else if (cnt == 9) {return 0;}}return 0;
}
總結
以上是生活随笔為你收集整理的C++ struct实现顺序表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [蓝桥杯2016初赛]报纸页数-生活常识
- 下一篇: C++实现双向链表