C++:顺序表的基本操作(待完善)
生活随笔
收集整理的這篇文章主要介紹了
C++:顺序表的基本操作(待完善)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
根據(jù)命令提示進(jìn)行順序表的基本操作(待完善)
#include<iostream> #include<algorithm> using namespace std; #define MAXSIZE 1000 #define OVERFLOW -2 #define ERROR 1 typedef int ElemType; typedef int Status; typedef struct {ElemType* elem;int length; }SqList; void InitList(SqList& L) {L.elem = new ElemType[MAXSIZE];if (L.elem == NULL){cout << "存儲(chǔ)空間分配失敗!" << endl;exit(OVERFLOW);}L.length = 0;cout << "順序表初始化完成" << endl; } void Create(SqList& L, int n, ElemType e) {cout << "請輸入" << n << "個(gè)順序表中的元素" << endl;for (int i = 0; i < n; i++) {ElemType e;cin >> e;L.elem[i] = e;L.length++;} } int Print(SqList L) {if (L.length == 0){cout << "順序表中無元素" << endl;return 1;}for (int k = 0; k < L.length; k++) {if (k == L.length - 1){cout << L.elem[k];}else {cout << L.elem[k] << ' ';}} } int Insert(SqList& L, int i, ElemType e) {cout << "請輸入要插入的元素及插得的位置" << endl;cin >> e >> i;if ((i < 1) || (i > L.length + 1)) {cout << "插入地址不合法" << endl;return ERROR;}if (L.length == MAXSIZE) {cout << "存儲(chǔ)空間已滿" << endl;return ERROR;}for (int j = L.length - 1; j >= i - 1; j--){L.elem[j + 1] = L.elem[j];}L.elem[i - 1] = e;++L.length; } int Delete(SqList& L, int i) {cout << "請輸入要?jiǎng)h除元素的位置" << endl;cin >> i;if((i < 1) || (i > L.length + 1)){cout << "刪除地址不合法" << endl;return ERROR;}for (int j = i; j <= L.length; j++){L.elem[j - 1] = L.elem[j];}--L.length; } void Sort(SqList& L) {sort(L.elem, L.elem + L.length); } int main() {int n, x,i=0,p=0;cout << "請輸入數(shù)組長度n的值" << endl;cin >> n;SqList L;ElemType e=0;InitList(L);do{cout << "請選擇您想進(jìn)行的操作" << endl;cout << "0.給順序表讀入值" << endl;cout << "1.給順序表插入值" << endl;cout << "2.給順序表刪除值" << endl;cout << "3.給順序表排序" << endl;cout << "4.輸出順序表" << endl;cin >> x; switch (x) {case 0: {Create(L, n, e);cout << "退出請按1,輸入其他數(shù)字返回上級清單" << endl;cin >> p;break; }case 1: {Insert(L, i, e);cout << "退出請按1,輸入其他數(shù)字返回上級清單" << endl;cin >> p;break;}case 2: {Delete(L, i);cout << "退出請按1,輸入其他數(shù)字返回上級清單" << endl;int p;cin >> p;break;}case 3: {Sort(L);cout << "退出請按1,輸入其他數(shù)字返回上級清單" << endl;cin >> p;break;}case 4: {Print(L);cout << "退出請按1,輸入其他數(shù)字返回上級清單" << endl;cin >> p;break;}}} while (p!=1); }總結(jié)
以上是生活随笔為你收集整理的C++:顺序表的基本操作(待完善)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++:录入班级数学成绩,计算最大值、平
- 下一篇: python:字典,元组