2020.10.1

  1 #include<iostream>
  2 using namespace std;
  3 #define MAXSIZE 100
  4 #define OK 1
  5 #define ERROR 0
  6 typedef int status;
  7 typedef struct
  8 {
  9     int a;
 10 }pta;
 11 typedef struct{
 12     pta *elem;
 13     int length;
 14 }SqList;
 15 status IniList(SqList &L)//构造空顺序表
 16 {
 17     L.elem=new pta[MAXSIZE];
 18     if(!L.elem)return ERROR;
 19     L.length=0;
 20     return OK;
 21 }
 22 status GetElem(SqList L,int i,pta &e)//获取
 23 {
 24     if(i<1||i>L.length)
 25         return ERROR;
 26     e=L.elem[i-1];
 27     return OK;
 28 }
 29 int LocateElem(SqList L,pta e)//查找
 30 {
 31     for(int i=0;i<L.length;i++)
 32         if(L.elem[i].a==e.a)
 33             return i+1;
 34         return 0;
 35 }
 36 status ListInsert(SqList &L,int i,pta e)//插入
 37 {
 38     if((i<1)||(i>L.length+1))return ERROR;
 39     if(L.length==MAXSIZE)return ERROR;
 40     for(int j=L.length-1;j>=i-1;j--)
 41         L.elem[j+1]=L.elem[j];
 42     L.elem[i-1]=e;
 43     ++L.length;
 44     return OK;
 45 }
 46 status ListDelete(SqList &L,int i)//删除
 47 {
 48     if((i<1)||(i>L.length))return ERROR;
 49     for(int j=i;j<=L.length-1;j++)
 50         L.elem[j-1]=L.elem[j];
 51     --L.length;
 52     return OK;
 53 }
 54 void display(SqList &L)
 55 {
 56     for(int i=0;i<L.length;i++)
 57         cout<<L.elem[i].a<<endl;
 58 }
 59 int main()
 60 {
 61     int i;
 62     SqList L;
 63     IniList(L);
 64     int mount;
 65     pta e;
 66     for(;;)
 67     {
 68     cout<<"遍历0,查询请输入1,查找输入2,插入输入3,删除输入4,退出输入5"<<endl;
 69     cin>>mount;
 70     if(mount==5)
 71         return 0;
 72     switch(mount)
 73     {
 74     case 0:
 75         display(L);break;
 76     case 1:
 77         cout<<"请输入你想获取第几个的数据"<<endl;
 78         cin>>i;
 79         GetElem(L,i,e);
 80         cout<<"其数据为:"<<e.a;break;
 81     case 2:
 82         cout<<"请输入你想查找的数据"<<endl;
 83         cin>>e.a;
 84         if(LocateElem(L,e)!=0)
 85         {
 86             cout<<"该数在第"<<LocateElem(L,e)<<""<<endl;
 87         }
 88         break;
 89     case 3:
 90         cout<<"请输入你想要插入到第几个位置"<<endl;
 91         cin>>i;
 92         cout<<"输入插入的数值为:";
 93         cin>>e.a;
 94         ListInsert(L,i,e);
 95         break;
 96 
 97     case 4:
 98         cout<<"请输入你想删除第几个数据"<<endl;
 99         cin>>i;
100         ListDelete(L,i);
101         break;
102     }
103     
104 }
105 }
原文地址:https://www.cnblogs.com/Nojava/p/13759082.html