c++实现线性表中的顺序表(数据结构课程作业)

一、题目简介

实现顺序表的创建、初始化、赋值、插入、删除、按数据查找元素位置、按元素位置查找数据、清空、删除

的操作,以上操作用可用菜单选项完成

二、源程序代码

 

#include
#define MAXSIZE 100
using namespace std;
   typedef int ElemType;
   typedef struct List
   {
      ElemType *elem;
      int length;
   }Sqlist;
   void InitList(Sqlist &L);
   void DestroyList(Sqlist &L);
   void ClearList(Sqlist &L);
   void GetLength(Sqlist L);
   void isEmpty(Sqlist L);
   void Getelem(Sqlist &L,int i);
   void Locateelem(Sqlist &L,ElemType e);
   void ListInsert(Sqlist &L,int i,ElemType e);
   void ListDelete(Sqlist &L,int i);
   void fuzhi(Sqlist &L);
   void print(Sqlist &L);
   void mainscreen();
   int main()
   {
     mainscreen();
     return 0;
   }
  void InitList(Sqlist &L)
   {
    L.elem=new ElemType[MAXSIZE];
    if(!L.elem){
       cout<<"内存空间分配失败"<<endl;
    }
    L.length=0;
      cout<<"顺序表初始化成功"<<endl;
   }
   void mainscreen()
  {
    int flag=0;
    while(flag==0)
    {
    cout<<"请选择对顺序表的操作"<<endl;
    cout<<"\t1 初始化顺序表"<<endl;
    cout<<"\t2 给顺序表赋值"<<endl;
    cout<<"\t3 向顺序表中插入数据元素"<<endl;
    cout<<"\t4 向顺序表中删除数据元素"<<endl;
    cout<<"\t5 清空顺序表"<<endl;
    cout<<"\t6 删除顺序表"<<endl;
    cout<<"\t7 根据数据元素查询在顺序表中位置"<<endl;
    cout<<"\t8 根据在顺序表中位置查询数据元素"<<endl;
    cout<<"\t9 查询顺序表长度"<<endl;
    cout<<"\t10 输出顺序表"<<endl;
    cout<<"\t11 退出操作"<<endl;
    int s;
    cin>>s;
    if(1<=s<=10)
    {
    switch(s)
    {
        case 1:
        Sqlist L;
        InitList(L);
        break;
        case 2:
        fuzhi(L);
        break;
        case 3:
        int a;
        ElemType e;
        cout<<"请输入你要插入的数据元素位置"<<endl;
        cin>>a;
        cout<<"请输入你要插入的数据元素"<<endl;
        cin>>e;
        ListInsert(L,a,e);
        break;
        case 4:
        int b;
        cout<<"请输入你要删除的数据元素位置"<<endl;
        cin>>b;
        ListDelete(L,b);
        break;
        case 5:
        ClearList(L);
        break;
        case 6:
        DestroyList(L);
        break;
        case 7:
        ElemType f;
        cout<<"请输入你要查找的数据元素"<<endl;
        cin>>f;
        Locateelem(L,f);
        break;
        case 8:
        int c;
        cout<<"请输入你要查找的数据元素的位置"<<endl;
        cin>>c;
        Getelem(L,c);
        break;
        case 9:
        GetLength(L);
        break;
        case 10:
        print(L);
        break;
        case 11:
        flag=1;
        cout<<"已成功退出对顺序表的操作"<<endl;
        exit(0);
        break;
    }
    }
    }
  }
   void DestroyList(Sqlist &L)
   {
       if(L.elem)
       {
       delete[] L.elem;
       cout<<"顺序表删除成功"<<endl;
       }
       else{
            cout<<"顺序表删除失败"<<endl;
       }
   }
   void ClearList(Sqlist &L)
   {
      L.length=0;
      cout<<"顺序表清空成功"<<endl;
   }
   void GetLength(Sqlist L)
   {
       cout<<"顺序表中有"<"个元素"<<endl;
   }
   void isEmpty(Sqlist L)
   {
     if(L.length==0)
     cout<<"顺序表为空"<<endl;
     else
     cout<<"顺序表不为空"<<endl;
   }
    void Getelem(Sqlist &L,int i)
    {
        ElemType e;
        if(i<1||i>L.length){
        cout<<"输入的查找参数错误"<<endl;
        }
        else{
           e=L.elem[i-1];
           cout<<"查找成功"<<endl;
           cout<<"顺序表中第"<"个数据元素是"<endl;
        }
    }
    void Locateelem(Sqlist &L,ElemType e)
    {
        int i=0;
        int flag=0;
        for(i=0;i)
        {
            if(L.elem[i]==e){
            cout<<"查找成功"<<endl;
            cout<"位于顺序表中第"<1<<"个数据元素"<<endl;
            flag=1;
            }
        }
            if(flag==0)
            cout<<"查找失败"<<endl;
    }
   void ListInsert(Sqlist &L,int i,ElemType e)
   {
       int j=0;
       if(i<1||i>L.length)
       {
        cout<<"输入的查找参数错误"<<endl;
       }
       if(L.length==MAXSIZE)
       {
          cout<<"当前储存空间已满,无法插入"<<endl;
       }
       for(j=L.length-1;j>=i-1;j--)
       {
         L.elem[j+1]=L.elem[j];
       }
         L.elem[i-1]=e;
        ++L.length;
       cout<<"插入顺序表中第"<"个数据元素成功"<<endl;
   }
  void ListDelete(Sqlist &L,int i)
  {
        int j=0;
       if(i<1||i>L.length)
       {
           cout<<"输入的查找参数错误"<<endl;
       }
       for(j=i;j<=L.length-1;j++)
       {
           L.elem[j-1]=L.elem[j];
       }
        --L.length;
      cout<<"删除顺序表中第"<"个数据元素成功"<<endl;
  }
  void fuzhi(Sqlist &L)
  {
      int n,m;
      cout<<"请输入赋值的数据元素个数"<<endl;
      cin>>n;
    for(int i=0;i)
    {
    cout<<"请输入第"<1<<"的数据元素的值"<<endl;
    cin>>m;
    L.elem[i]=m;
    L.length++;
    }
    cout<<"赋值成功"<<endl;
  }
  void print(Sqlist &L)
  {
    for(int i=0;i)
    {
       cout<<""<1<<"的数据元素的值:";
       cout<endl;
    }
    cout<<"顺序表输出完成"<<endl;
  }

 

你可能感兴趣的:(c++实现线性表中的顺序表(数据结构课程作业))