408数据结构——线性表之C++实现顺序表

可修改main函数内的注释,测试相应的顺序表操作。

#include <iostream>

using namespace std;
#define initSize 10  // 顺序表初始容量
    
    // 创建顺序线性表(顺序表),动态分配内存空间地址
    typedef struct {
    
    
        int *array;  // 指向顺序表的指针
        int maxSize,length; // 顺序表最大容量,顺序表当前长度
    }sqList;

    // 初始化顺序表
    void initSqList(sqList &L) {
    
    
        L.array = new int[initSize];
        L.maxSize = initSize;
        L.length = 0;
    }

    // 增加顺序表长度
    bool increaseSqList(sqList &L,int length) {
    
    
        if (length < 1)  return false;  // 增加长度小于1,则返回失败

        int *temp = L.array;    // 暂存原数据
        L.array = new int[initSize + length];    // 给新的顺序表分配新的内存空间地址
       
        for (int i = 0; i < L.length; i++) {
    
    
            L.array[i] = temp[i];
        }

        L.maxSize += length;
        delete(temp);
        return true;
    }

    // 指定第index个位置插入元素
    bool insertElement(sqList &L,int index,int e) {
    
    
        if (index < 1 || index > L.length+1) return false;
        if (L.length >= L.maxSize) return false;

        for (int i = L.length; i >= index; i--) {
    
       // 第index个元素及之后的元素都后移一位
            L.array[i+1] = L.array[i];
        }

        L.array[index-1] = e;
        L.length += 1;
        return true;
    }

    // 指定第index个位置删除元素
    bool deleteElement(sqList &L,int index,int &e) {
    
    
        if (index < 1 || index > L.length+1) return false;

        e = L.array[index-1];
        for (int i = index; i <= L.length; i++) {
    
       // 第index个元素及之后的元素都前移一位
            L.array[i-1] = L.array[i];
        }

        L.length -= 1;
        return true;
    }

    // 查询第index个位置的元素
    bool selectElementByIndex(sqList &L,int index,int &e) {
    
    
        if (index < 1 || index > L.length+1) return false;

        e = L.array[index-1];
        return true;
    }

    // 查询第一个值为value的元素的位置
    bool selectElementByValue(sqList &L,int value,int &index) {
    
    
        for (int i = 0; i < L.length; i++) {
    
    
            if (value == L.array[i]) {
    
    
                index = (i+1);
                return true;
            }
        }        
        return false;
    }

    int main() {
    
    
        sqList L;
        initSqList(L);

        // 测试插入元素操作
        // int index,e;
        // cout<<"请输入插入的位置"<<endl;
        // cin>>index;
        // cout<<"请输入插入的元素"<<endl;
        // cin>>e;      
        // if (insertElement(L,index,e)) {
    
    
        //     for (int i = 0; i < L.length; i++) {
    
    
        //          cout<<"值为"<<L.array[i]<<endl;
        //     }
        // } else {
    
    
        //      cout<<"插入失败"<<endl;
        // }

        // 测试扩展顺序表操作
        // for (int i = 0; i < L.maxSize; i++) {   // 填满顺序表
        //     insertElement(L,i+1,i*10);
        // }
        // cout<<"当前顺序表长度:"<<L.length<<endl;
        // for (int i = 0; i < L.length; i++) {
    
    
        //     if (i != L.length -1) {
    
    
        //         cout<<"值为:"<<L.array[i]<<",";
        //     } else {
    
    
        //         cout<<"值为:"<<L.array[i]<<endl;
        //     }           
        // }
        // int index1,e1,length,index2,e2;
        // cout<<"请输入插入的位置"<<endl;
        // cin>>index1;
        // cout<<"请输入插入的元素"<<endl;
        // cin>>e1;    

        // if (insertElement(L,index1,e1)) {
    
    
        //    cout<<"插入成功,当前长度为:"<<L.length<<endl;
        // } else {
    
    
        //     cout<<"超出总容量,当前长度为:"<<L.length<<endl;
        //     cout<<"请输入顺序表需增加的长度"<<endl;
        //     cin>>length;
        //     while (!increaseSqList(L,length))
        //     {
    
    
        //         cout<<"增加失败"<<endl;
        //         cout<<"请输入顺序表需增加的长度"<<endl;
        //         cin>>length;
        //     }           
        //     cout<<"增加成功,长度为:"<<L.maxSize<<endl;

        //     cout<<"请输入插入的位置"<<endl;
        //     cin>>index2;
        //     cout<<"请输入插入的元素"<<endl;
        //     cin>>e2; 

        //     while (!insertElement(L,index2,e2))
        //     {
    
    
        //         cout<<"插入失败,当前长度为:"<<L.length<<endl;
        //         cout<<"请输入插入的位置"<<endl;
        //         cin>>index2;
        //         cout<<"请输入插入的元素"<<endl;
        //         cin>>e2; 
        //     }
        //     cout<<"插入成功,当前长度为:"<<L.length<<endl;               
        //     for (int i = 0; i < L.length; i++) {
    
    
        //         if (i != L.length -1) {
    
    
        //             cout<<"值为:"<<L.array[i]<<",";
        //         } else {
    
    
        //             cout<<"值为:"<<L.array[i]<<endl;
        //         }           
        //     }
        // }

        // 测试删除元素操作
        // for (int i = 0; i < L.maxSize; i++) {   // 填满顺序表
        //     insertElement(L,i+1,i*10);
        // }
        // cout<<"当前顺序表长度:"<<L.length<<endl;
        // for (int i = 0; i < L.length; i++) {
    
    
        //     if (i != L.length -1) {
    
    
        //         cout<<"值为:"<<L.array[i]<<",";
        //     } else {
    
    
        //         cout<<"值为:"<<L.array[i]<<endl;
        //     }           
        // }
        // int index3,e3;
        // cout<<"请输入要删除的元素位置"<<endl;
        // cin>>index3; 
        // while (!deleteElement(L,index3,e3)) {
    
    
        //     cout<<"删除失败"<<endl;
        //     cout<<"请输入要删除的元素位置"<<endl;
        //      cin>>index3; 
        // }
           
        // cout<<"删除成功,删除的元素为:"<<e3<<endl;
        // cout<<"当前顺序表长度:"<<L.length<<endl;
        // for (int i = 0; i < L.length; i++) {
    
    
        //     if (i != L.length -1) {
    
    
        //         cout<<"值为:"<<L.array[i]<<",";
        //     } else {
    
    
        //         cout<<"值为:"<<L.array[i]<<endl;
        //     }           
        // }           

        // 测试查询第index个位置的元素操作
        // for (int i = 0; i < L.maxSize; i++) {   // 填满顺序表
        //     insertElement(L,i+1,i*10);
        // }
        // cout<<"当前顺序表长度:"<<L.length<<endl;
        // for (int i = 0; i < L.length; i++) {
    
    
        //     if (i != L.length -1) {
    
    
        //         cout<<"值为:"<<L.array[i]<<",";
        //     } else {
    
    
        //         cout<<"值为:"<<L.array[i]<<endl;
        //     }           
        // }

        // int index4,e4;
        // cout<<"请输入要查询的元素位置"<<endl;
        // cin>>index4; 
        // while (!selectElementByIndex(L,index4,e4)) {
    
    
        //     cout<<"查询失败"<<endl;
        //     cout<<"请输入要查询的元素位置"<<endl;
        //     cin>>index4; 
        // }           
        // cout<<"查询成功,元素为:"<<e4<<endl;
    

        // 测试查询第一个值为value的元素的位置操作
        for (int i = 0; i < L.maxSize; i++) {
    
       // 填满顺序表
            insertElement(L,i+1,i*10);
        }
        cout<<"当前顺序表长度:"<<L.length<<endl;
        for (int i = 0; i < L.length; i++) {
    
    
            if (i != L.length -1) {
    
    
                cout<<"值为:"<<L.array[i]<<",";
            } else {
    
    
                cout<<"值为:"<<L.array[i]<<endl;
            }           
        }

        int index5,e5;
        cout<<"请输入要查询的元素的值"<<endl;
        cin>>e5; 
        while (!selectElementByValue(L,e5,index5)) {
    
    
            cout<<"查询失败"<<endl;
            cout<<"请输入要查询的元素的值"<<endl;
            cin>>e5; 
        }           
        cout<<"查询成功,该元素的位置为:"<<index5<<endl;
               
        return 0;
    }

猜你喜欢

转载自blog.csdn.net/long99920/article/details/124905965