顺序表的实现(基于C++)

#include<iostream>
#include<iomanip>
using namespace std; 

#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100

typedef int ElemType;
typedef int Status;


typedef struct{
    
    
    ElemType *elem;
    int length;
}SqList;

Status InitList(SqList &L)  //初始化
{
    
    
    L.elem=new int[MAXSIZE];
    if(L.elem==NULL)
       exit(OVERFLOW);
    L.length=0;
    return OK;
}

Status GetElem(SqList L,int i,ElemType &e) //按位取值
{
    
    
    if(i<0||i>=L.length)
       return ERROR;
    e=L.elem[i-1];
    return OK;
}

Status LocateElem(SqList L,ElemType e ) //查找
{
    
    
    int i=0;
    for(int j=i;j<L.length;j++)
    {
    
    
       if(e==L.elem[j])
           return j+1;
    }
    return 0;
}

Status ListInist(SqList &L,int i,ElemType e) //插入
{
    
    
	if(i==L.length+1){
    
    //在最后面插入一个数据
		L.elem[i-1]=e;
		L.length++;
		return OK;
	}
    if(i<0||i>L.length)
       return ERROR;
    if(L.length==MAXSIZE)
       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++;
    return OK;
}

Status ListDelete(SqList &L,int i)  //删除
{
    
    

    if(i<0||i>L.length)
       return ERROR;
    
    for(int j=i;j<=L.length-1;j++)
    {
    
    
       L.elem[j-1]=L.elem[j];
    }

    L.length--;
    return OK;
}

Status InputList(SqList &L,int n)  //输入
{
    
    

    for(int j=0;j<n;j++)
    {
    
    
       cin>>L.elem[j];
       L.length++;
    }

    return OK;
}

Status OutputList(SqList L) //输出
{
    
    

    int i;

    for(i=0;i<L.length;i++)
    {
    
    
       cout<<left<<setw(5)<<L.elem[i];
    }

    return OK;
}

int main()

{
    
    
    cout<<"1.初始化 2.输入 3.取值 4.查找 5.插入 6.删除 7.输出 0.退出";
    cout<<endl;
    SqList L;
    int choose=1,n;
    ElemType e;

    while(choose!=0)
    {
    
    
       cout<<endl;
       cout<<"请选择:";
       cin>>choose;
       switch(choose)
       {
    
    
           case 1:{
    
    
                     if(InitList(L))
                         cout<<"初始化成功,成功建立顺序表"<<endl;
                     else
                         cout<<"初始化失败"<<endl;
                     break;
                }

           case 2:{
    
    
                     cout<<"请输入数据的位数:";
                     cin>>n;
                     cout<<"请输入数据:"<<endl;
                     if(InputList(L,n))
                         cout<<"输入成功"<<endl;
                     else
                         cout<<"输入失败"<<endl;
                     break;
                }

           case 3:{
    
    
                     cout<<"请输入取值的位置:";
                     cin>>n;
                     if(GetElem(L,n,e))
                         cout<<"取值成功,第"<<n<<"个为:"<<e<<endl;
                     else
                         cout<<"取值失败"<<endl;
                     break;
                }

           case 4:{
    
    
                     cout<<"请输入查找的数据:";
                     cin>>e;
                     if(LocateElem(L,e))
                         cout<<"查找成功,第"<<LocateElem(L,e)<<"个为:"<<e<<endl;
                     else
                         cout<<"查找失败"<<endl;
                     break;
                }

           case 5:{
    
    
                     cout<<"请输入插入的位置:";
                     cin>>n;
                     cout<<"请输入插入的数据:";
                     cin>>e;
                     if(ListInist(L,n,e) )
                         cout<<"插入成功"<<endl;
                     else
                         cout<<"插入失败"<<endl;
                     cout<<"当前数据为:";
                     OutputList(L);
                     cout<<endl;
                     break;
                }

           case 6:{
    
    
                     cout<<"请输入删除的位置:";
                     cin>>n;
                     if(ListDelete(L,n) )
                         cout<<"删除成功"<<endl;
                     else
                         cout<<"删除失败"<<endl;
                     cout<<"当前数据为:";
                     OutputList(L);
                     cout<<endl;
                     break;
                }

           case 7:{
    
    
                     cout<<"当前顺序表数据为:"; 
                     OutputList(L);
                     cout<<endl;
                     break;
                }
       }
    }

    return 0;

}



在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46438851/article/details/122440877