C++之顺序表的基本操作

实验内容与步骤
(1)建立第一个项目,编程实现顺序表的基本操作,要求运行时先出现类似如下的菜单项,程序会根据输入的数字自动调用相关的功能函数:
" 请输入相关选项:"
" 1.在表尾添加新成员"
" 2.在指定位置前插入新成员"
" 3.删除指定位置的成员"
" 4.输出指定位置处成员信息"
" 5.输出所有成员信息"
" 6.输出表中成员个数"
" 7.合并两个有序顺序表"


#include <iostream>


using namespace std;
#define Maxsize 100//最大空间
typedef struct
{
    int *elem;
    int length;//顺序表长度

} SqList;

bool InitList(SqList &L) //构造一个空的顺序表L
{
    L.elem = new int[Maxsize];
    L.length = 0;
    return true;
}

bool CreateList(SqList &L,int a)//创建一个顺序表L
{
    int	i;
    int p;
    for(i=0; i<a; i++)
    {
        cout<<"请输入第"<<i+1;
        cout<<"个元素";
        cin>>p;
        L.elem[i]=p;
        L.length++;
    }

    return true;
}

bool GetElem(SqList L)
{
    int e,i;
    cout << "输入整型数i,取第i个元素输出" << endl;
    cin >> i;
    if (i<1 || i>L.length)
    {
        return false;
    }
    e = L.elem[i - 1];//第i-1的单元存储着第i个数据

    cout << "第i个元素是:" << e << endl;

    return true;

}
void AddElem(SqList &L)
{
    int m;
    int n=L.length;
    cout<<"请输入要添加到元素"<<endl;
    cin>>m;
    L.elem[n]=m;
    L.length++;
}


bool InsertSqList(SqList &L)
{
    int i,e;
    cout << "请输入要插入的位置和要插入的数据元素e:";
    cin >> i >> e;
    if (i<1 || i>L.length + 1)
    {
        return false;//i值不合法
    }
    if (L.length == Maxsize)
    {
        return false;//存储空间已满
    }
    for (int j = L.length - 1; j >= i - 1; j--)
    {
        L.elem[j + 1] = L.elem[j];//从最后一个元素开始后移,直到第i个元素后移
    }
    L.elem[i - 1] = e;//将新元素e放入第i个位置
    L.length++;//表长加1
    return true;
}

bool DeleteSqList(SqList &L)
{

    int i;
    cout << "请输入要删除的位置i:";
    cin >> i;
    if ((i < 1) || (i > L.length))
    {
        return false;//i值不合法
    }
    for (int j = i; j <= L.length - 1; j++)
    {
        L.elem[j - 1] = L.elem[j];//被删除元素之后的元素后移
    }
    L.length--;//表长减少1
    return true;
}

void print(SqList &L)
{
    cout << "输出顺序表" << endl;
    for (int j = 0; j <= L.length - 1; j++)
        cout << L.elem[j] << " ";
    cout << endl;
}
void PrintNum(SqList &L)
{
    int n=L.length;
    cout<<"顺序表的元素个数为"<<n;
}
void DestroyList(SqList &L)
{
    if (L.elem)
    {
        delete[]L.elem;//释放存储空间
    }

}
void Sort(SqList &L)
{
    int i,j;
    int temp;
    for(i=0;i<L.length;i++)
    {
        for(j=i+1;j<L.length;j++)
        {
            if(L.elem[i]<L.elem[j])
            {
                temp=L.elem[i];
                L.elem[i]=L.elem[j];
                L.elem[j]=temp;
            }
        }
    }

}
void mergelist(SqList &L,SqList &S,SqList &Sq)
{
  int i=0,j=0,k=0;
  while((i<=L.length)&&(j<=S.length))
  {
      if(L.elem[i]<=S.elem[j])
      {
        Sq.elem[k]=L.elem[i];
        i++;
        k++;
      }
      else
      {
        Sq.elem[k]=S.elem[j];
        j++;
        k++;
      }
  }
  while(i<=L.length)
  {
      Sq.elem[k]=L.elem[i];
      i++;
      k++;
  }
  while(j<=S.length)
  {
    Sq.elem[k]=S.elem[j];
    j++;
    k++;
  }
  Sq.length=L.length+S.length+1;
}

int main()
{
    SqList L;
    SqList S;
    SqList Sq;
    cout << "1.在表尾添加新成员\n";
    cout << "2.在指定位置前插入新成员 \n";
    cout << "3.删除指定位置的成员\n";
    cout << "4.输出指定位置处成员信息\n";
    cout << "5.输出所有成员信息\n";
    cout << "6.输出表中成员个数\n";
    cout << "7.合并两个有序顺序表\n";
    cout << "8.销毁\n";
    cout << "0.退出\n";
    int num = 0;

    int choose = -1;
    InitList(L);
    InitList(S);
    InitList(Sq);
    cout<<"请输入顺序表的元素个数"<<endl;
    cin>>num;
    CreateList(L,num);
    while (choose != 0)
    {
        cout << "请选择:";
        cin >> choose;
        switch (choose)
        {

        case 1:
            AddElem(L);
            break;
        case 2:
            if (InsertSqList(L))
                cout << "插入成功!" << endl;
            else
                cout << "插入失败!" << endl;
            break;

        case 3:

            if (DeleteSqList(L))
                cout << "删除成功!" << endl;
            else
                cout << "删除失败!" << endl;
            break;
        //查找
        case 4:

            if (GetElem(L))
            {
                //cout << "第i个元素是:" << e << endl;
            }
            else
                cout << "顺序表取值失败!" << endl;;
            //cout << "第i个元素是:" << e << endl;
            break;

        case 5:
            print(L);
            break;

        case 6:
            PrintNum(L);
            break;

        case 7:
            CreateList(S,num);
            Sort(L);
            Sort(S);
            mergelist(L,S,Sq);
            print(Sq);
            break;

        case 8:
            cout << "顺序表销毁·····" << endl;
            DestroyList(L);
            break;


        }

    }
    return 0;
}

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼跳跳.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值