创建一个顺序表存储整型数据,并分别实现查找、插入、删除。每次操作前后,均需要输出显示。

#include <iostream>
using namespace std;
 
#define Maxsize 100
struct SqList
{
    int *elem;
    int length;
} ;
 
int InitList(SqList &L)
{//构造一个空的顺序表
    L.elem=new int[Maxsize];
    if(!L.elem)
	{
        return false;
    }
    L.length=0;
    return true;
}
 
int CreateList(SqList &L)
{
    int a,i=0;
    cin>>a;
    while(a!=-1)
	{
        if(L.length==Maxsize)
		{
            cout<<"顺序表满了!";
            return  false;
        }
        L.elem[i++]=a;
        L.length++;
        cin>>a;
    }
    return true;
}
//顺序表取值 
int GetElem(SqList L,int i,int &e)
{
    if(i<0|| i>L.length) 
	{
		return false;
	}
    e=L.elem[i-1];
}
//顺序表查找 
int LocateElem(SqList L,int e)
{
    for(int i=0;i<L.length;i++)
        if(L.elem[i]==e) return i+1;
 
    return 0;
}
//顺序表插入 
int ListInsert(SqList &L,int i,int e)
{
    if(i<1||i>L.length+1)
	{
        return false;
    }
    if(L.length==Maxsize)
	{
        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;
}
 

 
int ListDelete(SqList &L ,int i,int &e)
{
    if(i<1||i>L.length)
	{
        return false;
    }
    for(int j=i;j<=L.length-1;j++)
	{
        L.elem[j-1]=L.elem[j];
    }
    L.length--;
    return true;
 
}
 

int main()
{
    SqList myL;
    int i,e,x;
    cout << "1. 查找\n";
    cout << "2. 插入\n";
    cout << "3. 删除\n";
    int choose=-1;
 
 
    while(choose!= 0)
    {
        cout<<("请选择:");
        cin>>choose;
        switch(choose)
        {
            case 1://查找
                cout<<"请输入要查找的数x:"<<endl;
 
                cin>>x;
                if(LocateElem(myL,x)==-1)
                    cout<<"FA"<<endl;
                else
                    cout<<"SU"<<endl;
 
                break;
            case 2://插入
                cout<<"请输入要插入的位置和要插入的数据元素e:";
                cin>>i>>e;
                if(ListInsert(myL,i,e))
                    cout<<"成功!"<<endl;
                else
                    cout<<"失败!"<<endl;
                break;
            case 3://删除
                cout<<"请输入要删除的第i个位置:"<<endl;
 
                cin>>i;
                if((ListDelete(myL,i,e))!=-1)
                    cout<<"SU:"<<endl;
                else
                    cout<<"FA"<<endl;
 
                break;
            
        }
    }
 
    return 0;
}
 
 
#include <iostream>
using namespace std;
 
#define Maxsize 100
struct SqList
{
    int *elem;
    int length;
} ;
 
int InitList(SqList &L)
{//构造一个空的顺序表
    L.elem=new int[Maxsize];
    if(!L.elem)
	{
        return false;
    }
    L.length=0;
    return true;
}
 


//顺序表查找 
int LocateElem(SqList L,int e)
{
    for(int i=0;i<L.length;i++)
        if(L.elem[i]==e) return i+1;
 
    return 0;
}
//顺序表插入 
int ListInsert(SqList &L,int i,int e)
{
    if(i<1||i>L.length+1)
	{
        return false;
    }
    if(L.length==Maxsize)
	{
        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;
}
 

//顺序表删除 
int ListDelete(SqList &L ,int i,int &e)
{
    if(i<1||i>L.length)
	{
        return false;
    }
    for(int j=i;j<=L.length-1;j++)
	{
        L.elem[j-1]=L.elem[j];
    }
    L.length--;
    return true;
 
}
 

int main()
{
    SqList myL;
    int i,e,x;
    cout << "1. 查找\n";
    cout << "2. 插入\n";
    cout << "3. 删除\n";
    int choose=-1;
 
 
    while(choose!= 0)
    {
        cout<<("请选择:");
        cin>>choose;
        switch(choose)
        {
            case 1://查找
                cout<<"请输入要查找的数x:"<<endl;
 
                cin>>x;
                if(LocateElem(myL,x)==-1)
                    cout<<"FA"<<endl;
                else
                    cout<<"SU"<<endl;
 
                break;
            case 2://插入
                cout<<"请输入要插入的位置和要插入的数据元素e:";
                cin>>i>>e;
                if(ListInsert(myL,i,e))
                    cout<<"成功!"<<endl;
                else
                    cout<<"失败!"<<endl;
                break;
            case 3://删除
                cout<<"请输入要删除的第i个位置:"<<endl;
 
                cin>>i;
                if((ListDelete(myL,i,e))!=-1)
                    cout<<"SU:"<<endl;
                else
                    cout<<"FA"<<endl;
 
                break;
            
        }
    }
 
    return 0;
}
 
 

5a3f84a6c1864ba5903b15c6b319b5a2.png09b51c83acf14ef385a6361c814e88bc.pngb9bc404ee8af4b67993fddb7808da846.png19f71707e705495f9e6756fa46bcb9ee.pngf55d513694a346be915ee652e998c4af.png5af2831eb97645a2944c1c0a1a500211.png051e8f278e5548cd9c2bad09706ab4c6.pnge45bf13ad6154aa98ae19e27e5c723c6.png

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值