数据结构-顺序表(C++实现)

#include<iostream>
using namespace std;
#define MAXLISTSIZE 100 //顺序表的最大容量

struct Sqlist
{
    int *elem;
    int length;
};

bool InitList(Sqlist &L){
    L.elem = new int[MAXLISTSIZE];
    if (!L.elem)
    {
        return false;
    }
    L.length = 0;
    return true;
}

bool CreateList(Sqlist &L){
    int a,i=0;
    cin>>a;
    while(a!=-1){
        if(L.length==MAXLISTSIZE){
            cout<<"顺序表已满! ";
            return false;
        }
        L.elem[i++]=a;
        L.length++;
        cin>>a;
    }
    return true;
}

void SelectNode(Sqlist &L){
    int i=0;
    cin>>i;
    if (i>L.length || i<1)
    {
        cout<<"Your input is over the length"<<endl;
    }
    else{
        cout<<"The "<<i<<" number is "<<L.elem[i-1]<<endl;
    }
}

void PrintList(Sqlist &L){
    for(int i=0;i<L.length;i++){
        cout<<L.elem[i]<<"-";
    }
    cout<<endl;
};

int FindValue(Sqlist &L, int value){
    for(int i=0;i<L.length;i++){
        if(L.elem[i]==value)
        {
            return i+1;
        }
    }
    return -1;
}

bool InsertNode(Sqlist &L, int location, int value){
    if (location<1||location>L.length+1)
    {
        cout<<"您输入的位置不合法。"<<endl;
        return false;
    }
    else if (L.length==MAXLISTSIZE)
    {
        cout<<"当前顺序表已达最大长度。"<<endl;
        return false;
    }
    else{
        for(int p=L.length-1;p>=location-1;p--){
            L.elem[p+1]=L.elem[p];
        }
        L.elem[location-1]=value;
        L.length++;
        return true;
    }   
    
}

bool DeleteNode(Sqlist &L, int location){
    if (location<1||location>L.length)
    {
        cout<<"您输入的位置不合法。";
        return false;
    }
    else{
        for(int x=location;x<L.length;x++){
            L.elem[x-1]=L.elem[x];
        }
        L.length--;
        return true;
    }
    
}

void DeleteList(Sqlist &L){
    L.length=0;
    cout<<"顺序表删除成功!"<<endl;
}

int main(){
    Sqlist L;
    cout<<"开始初始化顺序表..."<<endl;
    if (InitList(L))
    {
        cout<<"初始化顺序表L成功..."<<endl;
    }
        cout<<"1.创建\n";
        cout<<"2.取值\n";
        cout<<"3.查找\n";
        cout<<"4.插入\n";
        cout<<"5.删除\n";
        cout<<"6.输出\n";
        cout<<"7.销毁\n";
        cout<<"0.退出\n";
        int choose=-1;
        cin>>choose;
    while (choose!=0)
    {
        if (choose==1)
        {
            cout<<"顺序表创建····"<<endl;
            cout<<"输入整型数,输入-1结束"<<endl;
            if (CreateList(L))
            {
                cout<<"顺序表创建成功!"<<endl;
            }
            else{
                cout<<"顺序表创建失败!"<<endl;
                continue;
            }
        }
        else if (choose==6)
        {
            cout<<"当前的顺序表为";
            PrintList(L);
        }
        else if (choose==2)
        {
            cout<<"请输入你需要取的数据序号(从1开始)"<<endl;
            SelectNode(L);
        }
        else if (choose==3)
        {
            cout<<"请输入要查找的值"<<endl;
            int value=-1;
            cin>>value;
            int serial=FindValue(L,value);
            if (serial!=-1)
            {
                cout<<"The "<<value<<"'s sequence is "<<serial<<endl;
            }
            else{
                cout<<"The "<<value<<" is not in the List!"<<endl;
            }
            
        }
        else if (choose==4)
        {
            cout<<"请确认插入的位置:";
            int location=-1;
            cin>>location;
            cout<<"请输入插入的数值:";
            int value=-1;
            cin>>value;
            if(InsertNode(L,location,value)){
                cout<<"插入成功!"<<endl;
            }
            else{
                cout<<"插入失败!"<<endl;
            }
        }
        else if (choose==5)
        {
            cout<<"请输入需要删除的结点序号:";
            int location=-1;
            cin>>location;
            if(DeleteNode(L,location)){
                cout<<"删除结点成功"<<endl;
            }
            else{
                cout<<"删除结点失败"<<endl;
            }
        }
        else if (choose==7)
        {
            DeleteList(L);
        }
        
        
        
        
        
        cout<<"1.创建\n";
        cout<<"2.取值\n";
        cout<<"3.查找\n";
        cout<<"4.插入\n";
        cout<<"5.删除\n";
        cout<<"6.输出\n";
        cout<<"7.销毁\n";
        cout<<"0.退出\n";
        cin>>choose;
    }
    cout<<"程序已退出"<<endl;
    return 0;
}

posted @ 2021-07-21 14:16  宁宁鸡a  阅读(144)  评论(0编辑  收藏  举报