日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++:顺序表的基本操作(待完善)

發布時間:2025/4/16 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++:顺序表的基本操作(待完善) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

根據命令提示進行順序表的基本操作(待完善)

#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 << "存儲空間分配失敗!" << endl;exit(OVERFLOW);}L.length = 0;cout << "順序表初始化完成" << endl; } void Create(SqList& L, int n, ElemType e) {cout << "請輸入" << n << "個順序表中的元素" << 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 << "存儲空間已滿" << 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 << "請輸入要刪除元素的位置" << 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 << "請輸入數組長度n的值" << endl;cin >> n;SqList L;ElemType e=0;InitList(L);do{cout << "請選擇您想進行的操作" << 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,輸入其他數字返回上級清單" << endl;cin >> p;break; }case 1: {Insert(L, i, e);cout << "退出請按1,輸入其他數字返回上級清單" << endl;cin >> p;break;}case 2: {Delete(L, i);cout << "退出請按1,輸入其他數字返回上級清單" << endl;int p;cin >> p;break;}case 3: {Sort(L);cout << "退出請按1,輸入其他數字返回上級清單" << endl;cin >> p;break;}case 4: {Print(L);cout << "退出請按1,輸入其他數字返回上級清單" << endl;cin >> p;break;}}} while (p!=1); }

總結

以上是生活随笔為你收集整理的C++:顺序表的基本操作(待完善)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。