顺序表查删插以及合并


  1 #pragma GCC optimize(2)//O2
  2 #include
  3 using namespace std;
  4 const int M=10000;//最大多少个数组
  5 typedef struct node
  6 {
  7     int a[M];//数据域
  8     int Last;//末下标
  9 }*List;
 10 int n;
 11 List MakeEmpty(List L)//建立空表
 12 {
 13     L=(List)malloc(sizeof(struct node));
 14     L->Last=-1;
 15     return L;
 16 }
 17 int Find(List L,int x)//查找函数
 18 {
 19     int i=0;
 20     while(i<=L->Last&&L->a[i]!=x)
 21     i++;
 22     if(i>L->Last)
 23     return -1;
 24     else
 25     return i;
 26 }
 27 bool Insert(List L,int p,int i)//插入函数
 28 {
 29     int j;
 30     if(L->Last==M-1)
 31     {
 32         cout<<"表已经满了"<<endl;
 33         return false;
 34     }
 35     if(i<1||i>L->Last+2)
 36     {
 37         cout<<"位置不合法"<<endl;
 38         return false;
 39     }
 40     for(j=L->Last;j>=i-1;j--)
 41     {
 42         L->a[j+1]=L->a[j];
 43     }
 44     L->a[i-1]=p;
 45     L->Last++;
 46     return true;
 47 
 48 }
 49 bool Delete(List L,int r)//删除函数
 50 {
 51     int j;
 52     if(r<1||r>L->Last+1)
 53     {
 54         cout<<"要删除的元素不存在"<<endl;
 55         return false;
 56     }
 57     for(j=r;j<=L->Last;j++)
 58     {
 59         L->a[j-1]=L->a[j];
 60     }
 61     L->Last--;
 62     return true;
 63 }
 64 void merge(List A, List B, List& C)//合并算法
 65 {
 66     int i=0, j=0, k=0;
 67     while (i < A->Last && j < B->Last)
 68     {
 69         if(A->a[i]a[j])
 70         {
 71             C->a[k] = A->a[i];
 72             i++; k++;
 73         }
 74         else if (A->a[i] >B->a[j])
 75         {
 76             C->a[k] = B->a[j];
 77             j++; k++;
 78         }
 79         else
 80         {
 81             C->a[k] = B->a[j];
 82             j++; k++;
 83             C->a[k] = A->a[i];
 84             i++; k++;
 85         }
 86     }
 87     while(iLast)
 88     {
 89         C->a[k] = A->a[i];
 90         i++; k++;
 91     }
 92     while (j < B->Last)
 93     {
 94         C->a[k] = B->a[j];
 95         j++; k++;
 96     }
 97     C->Last = k;
 98 }
 99 int main()
100 {
101     ios::sync_with_stdio(false);//快输
102     List L=MakeEmpty(L);//建表
103     cin>>n;
104     for(register int i=0;i)
105     {
106         cin>>L->a[i];
107     }
108     L->Last=n-1;//把末下标确定下来
109     int x;
110     cin>>x;
111     if(Find(L,x)==-1)//判断条件,分找到和没找到
112     cout<<"没有找到您所要找的元素"<<endl;
113     else
114     {
115         cout<//如果查找到了就输出那个下表就可以了
116      }
117      int i;
118      int p;
119      cin>>p>>i;
120     Insert(L,p,i);//开始插入
121     cout<<"插入之后的元素是:"<<' ';
122     for(register int j=0;j<=L->Last;j++)//输出插入之后的元素都有那些
123     cout<a[j]<<' ';
124     cout<<endl;
125     int r;
126     cin>>r;
127     Delete(L,r);//开始删除
128     cout<<"删除之后的元素是:"<<' ';
129     for(register int j=0;j<=L->Last;j++)//输出删除之后的元素都有那些
130     {
131         cout<a[j]<<' ';
132      }
133      cout<//换行好看显美观
134     List sqA, sqB,sqC;
135     int Adata,Bdata;
136     cout << "请输入顺序表A中的元素个数(不超过100个)";
137     cin>>Adata;
138     sqA=MakeEmpty(sqA);
139     cout << "下面请按元素值递增顺序输入元素" << endl;
140     for (int i = 0; i < Adata; i++)
141     {
142         cout << "顺序表中A第" << i << "个元素是:";
143         cin >> sqA->a[i];
144     }
145     sqA->Last = Adata;
146     cout << "顺序表A的长度为:" << sqA->Last << endl;
147     cout << "请输入顺序表B中的元素个数(不超过100个)";
148     cin >> Bdata;
149     sqB=MakeEmpty(sqB);
150     for (int i = 0; i < Bdata; i++)
151     {
152         cout << "顺序表B中第" << i << "个元素是:";
153         cin >> sqB->a[i];
154     }
155     sqB->Last = Bdata;
156     cout << "顺序表B的长度为:" << sqB->Last << endl;
157     merge(sqA, sqB, sqC);
158     cout << "顺序表C的长度为:" << sqC->Last << endl;
159     cout << "顺序表C中的元素依次为:";
160     for (int i = 0; i < sqC->Last; i++)
161     {
162         cout <a[i]<6);
163     }
164 
165     return 0;
166 }