#include <iostream>
#include <stdlib.h>
#define MAX_SIZE 100
using namespace std;

typedef struct _SqList {
    int* elems; //顺序表的基地址
    int size; //顺序表的大小
    int length; //顺序表的长度
}SqList;

bool initSqList(SqList& L) { //初始化顺序表
    L.elems = new int[MAX_SIZE];
    if (!L.elems) {
        return false;
    }
    L.size = MAX_SIZE;
    L.length = 0;
    return true;
}
void PrintSqList(SqList& L) { //打印输出顺序表
    cout << "顺序表的长度为" << L.length << " ,顺序表的大小为" << L.size << endl;
    for (int i = 0; i < L.length; i++) {
        cout << L.elems[i] << " ";
    }
    cout << endl;
}
bool SqListAppend(SqList& L,int e) { //在顺序表尾部添加元素
    if (L.length == L.size) { //顺序表空间已满
        return false;
    }
    L.elems[L.length] = e;
    L.length++;
    return true;
}
bool SqListInsert(SqList& L, int i, int e) {  //在顺序表的指定位置插入元素
    if (i < 0 || i >= L.length) return false;
    if (L.length == L.size) return false;
    for (int j = L.length - 1; j >=i; j--) {
        L.elems[j + 1] = L.elems[j];
    }
    L.elems[i] = e;
    L.length++;
    return true;
}
bool SqListDeleteI(SqList& L, int i) { //删除顺序表中指定位置的元素
    if (!L.elems) return false;
    if (i < 0 || i >= L.length) return false;
    if (i == L.length - 1) { //直接删除顺序表最后一个元素
        L.length--;
        return true;
    }
    for (int j = i; j < L.length; j++) {
        L.elems[j] = L.elems[j+1]; //被删除元素后面的元素全部前移一位
    }
    L.length--;
    return true;
}
bool SqListDeleteE(SqList& L,int e) {  //删除顺序表中指定的所有元素
    if (!L.elems) return false;
    int n = 0;//记录不等于e的元素个数 
    for (int i = 0; i < L.length; i++) { //从前往后扫描 
        if (L.elems[i] != e) {
            L.elems[n] = L.elems[i];  //利用原表的空间记录值不为e的元素 
            n++;
        }
    }
    L.length = n;
    return true;
}
void SqListDestory(SqList& L) {  //销毁顺序表
    if (L.elems)  delete[] L.elems; //释放存储空间
    L.length = 0;
    L.size = 0;
}
int main() {
    SqList list; //创建顺序表list
    int e = 0;   //顺序表要插入的元素
    int count = 0; //顺序表要插入的元素个数
    int i = 0; //顺序表的元素位置

    //初始化顺序表
    if (initSqList(list)) {
        cout << "顺序表初始化成功!" << endl;
    }
    else {
        cout << "顺序表初始化失败!" << endl;
    }
    PrintSqList(list);

    //在顺序表尾部添加元素
    cout << "请输入要添加的元素个数:";
    cin >> count;
    for (int i = 0; i < count; i++) {
        cout << "请输入要添加的元素:";
        cin >> e;
        if (SqListAppend(list, e)) {
            cout << "添加成功!" << endl;
        }
        else {
            cout << "添加失败!" << endl;
        }
    }
    PrintSqList(list);

    //在顺序表的指定位置插入元素
    cout << "请输入要插入的元素位置和元素:";
    cin >> i >> e;
    if (SqListInsert(list, i, e)) {
        cout << "插入成功!" << endl;
    }
    else {
        cout << "插入失败!" << endl;
    }
    PrintSqList(list);

    //删除顺序表中指定位置的所有元素
    cout << "请输入要删除的元素位置:";
    cin >> i;
    int n = list.elems[i];
    if (SqListDeleteI(list, i)) {
        cout << "删除在"<< i <<"处的元素 " << n << " 成功" << endl;
    }
    else {
        cout << "删除失败" << endl;
    }
    PrintSqList(list);

    //删除顺序表中指定的元素
    cout << "请输入要删除的元素:";
    cin >> e;
    if (SqListDeleteE(list, e)) {
        cout << "删除成功" << endl;
    }
    else {
        cout << "删除失败" << endl;
    }
    PrintSqList(list);

    //销毁顺序表
    SqListDestory(list);

    system("pause");
    return 0;
}

posted on 2023-01-16 22:42  wshidaboss  阅读(677)  评论(0编辑  收藏  举报