C++学习---顺序表的构建及操作

#include<iostream>
#include<fstream>

using namespace std;
#define MAXLEN 100

//定义顺序表
struct Sqlist {
    int *elem;
    int length;
};
//初始化顺序表
void InitList(Sqlist &L) {
    L.elem = new int[MAXLEN];
    if (!L.elem) exit(OVERFLOW);
    L.length = 0;
}
//顺序表的取值 i代表取第几位  0代表索引输入错误
int GetElem(Sqlist L, int i) {
    if (i<1 || i>L.length) return 0;
    return L.elem[i - 1];
}


//顺序表的查找 e为需查找的值 返回值为0代表无
int LocateElem(Sqlist L, int e) {
    //遍历顺序表L,查找值为e的元素返回序号
    for (int i = 0; i < L.length; i++) {
        if (L.elem[i] == e) return ++i;
    }
    return 0;
}
//顺序表的插入 i为插入的位置  e为插入的值
bool ListInsert(Sqlist& L, int i, int e) {
    if (i<1 || i>L.length+1) return false;
    if (L.length == MAXLEN) 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;
}
//顺序表的删除  i为删除位置
bool ListDelete(Sqlist& L, int i) {
    if (i<1 || i>L.length) return false;
    for (int j = i; j <= L.length; j++) {
        L.elem[j - 1] = L.elem[j];
    }
    --L.length;
    return true;
}

//顺序表的输入 
void ListInput(Sqlist &L) {
    int length=0;
    cout << "请输入顺序表的长度:";
    cin >> L.length;
    for (int i = 0; i < L.length; i++) {
        cout << "请输入第" << i+1 << "个值:";
        cin >> L.elem[i];
    }
}

//顺序表的输出
void ListOutput(Sqlist &L) {
    cout << "顺序表的值依次为:";
    for (int i = 0; i < L.length; i++) {
        cout << L.elem[i] << "\t";
    }
    cout << endl;
}

int main() {
    int opearateNum = 0;//操作值
    Sqlist LA;
    //初始化
    InitList(LA);

    while (true)
    {
        int selectIndex=0;//增删查所用的索引
        int selectValue=0;//增查所用的值


        cout << "1、输入顺序表\t2、输出顺序表\t3、取值\t4、查值\t5、插入\t6、删除\t7、退出" << endl;
        cin >> opearateNum;
        if (opearateNum == 7)
            break;
        switch (opearateNum)
        {
        case 1:
            //数据输入
            ListInput(LA);
            system("pause");
            system("cls");
            break;
        case 2:
            //输出顺序表
            ListOutput(LA);
            system("pause");
            system("cls");
            break;
        case 3:
            
            cout << "输入要取第几位:";
            cin >> selectIndex;
            if (GetElem(LA, selectIndex) == 0)
                cout << "没有第" << selectIndex << "位的值" << endl;
            else
                cout << "第" << selectIndex << "位的值:" << GetElem(LA, selectIndex) << endl;
            system("pause");
            system("cls");
            break;
        case 4:
            cout << "输入要查找的值:";
            cin >> selectValue;
            if (LocateElem(LA, selectValue) == 0)
                cout << "没有该值!"<< endl;
            else
                cout << "值位" << selectValue << "的索引:" << LocateElem(LA, selectValue) << endl;
            system("pause");
            system("cls");
            break;
        case 5:
            cout << "输入要插入的位置和值(如:第2位插入值为4,输入:2 4):";
            cin >> selectIndex >> selectValue;
            if (ListInsert(LA, selectIndex, selectValue))
                cout << "插入成功!" << endl;
            else
                cout << "插入失败!" << endl;

            system("pause");
            system("cls");
            break;
        case 6:
            cout << "输入要删除第几位:";
            cin >> selectIndex;
            if (ListDelete(LA, selectIndex))
                cout << "删除成功!" << endl;
            else
                cout << "删除失败!" << endl;
            system("pause");
            system("cls");
            break;
        case 7:
            break;
        default:
            cout << "无效操作,请重新输入!"<<endl;
            break;
        }
        
    }    

    
    
}
 

  

posted @ 2020-09-12 17:12  edl李曉宇  阅读(690)  评论(0编辑  收藏  举报