实验目标:
(1)构造一个最大容量为LIST_INIT_SIZE的顺序表。通过InitList函数来实现
(2)在顺序表中查询第一个满足判定条件的数据元素,若存在,则返回他的位序,否则返回0。通过ListEmpty函数来实现
(3)在顺序表L的第i个元素之前插入新的元素,用InsertVlaue函数来实现
(4)删除顺序表L的第i个元素,用e返回其值,通过DeleteValue来实现
(5)已知两个顺序表A和B按元素值递增有序排序,要求写一算法实现将A和B归并成一个按元素值递增的有序排序的顺序表
实验代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
| #include <bits/stdc++.h> using namespace std;
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2
typedef int Status; typedef int ElemType;
#define LIST_INIT_SIZE 100 #define LISTINCREMENT 10
typedef struct List { ElemType *elem; int length; int listsize; }SqList;
Status InitList(SqList &L) { L.elem = (int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem) { cout<<"分配存储空间失败!"<<endl; exit(OVERFLOW); }
L.length=0; L.listsize=LIST_INIT_SIZE; return OK; }
Status InsertList(SqList &L) { int i,j; cout<<"请输入插入顺序表的元素的个数:"; cin>>i; cout<<endl;
if(i>L.listsize) { while(1) { if(i>L.listsize) { L.elem=(ElemType *)realloc(L.elem,LISTINCREMENT * sizeof(ElemType)); L.listsize += LISTINCREMENT; } else break; } }
cout<<"请输入元素:"; for(int j=1;j<=i;j++) { cin>>L.elem[j]; } L.length=i; cout<<endl; return OK; }
Status InsertValue(SqList &L,int i,ElemType e) { ElemType *newbase;
if(i<1||i>L.length) return ERROR;
if(L.length>=L.listsize) { newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) { cout<<"分配空间失败!"<<endl; exit(OVERFLOW); }
L.elem=newbase; L.listsize+=LISTINCREMENT; } L.length+=1;
for(int j=L.length;j>=i;j--) { L.elem[j]=L.elem[j-1]; } L.elem[i]=e; cout<<"插入成功!"<<endl; return OK; }
Status DeleteValue(SqList &L,int i) { if(i<1) { cout<<"删除失败,所要删除的位置不存在!"<<endl; exit(OVERFLOW); } if(i>L.length) { cout<<"删除失败,所要删除的位置超过顺序表长度!"<<endl; exit(OVERFLOW); }
cout<<"删除的元素为:"<<L.elem[i]<<endl; for(int j=i;j<L.length;j++) { L.elem[j]=L.elem[j+1]; }
L.length-=1; cout<<"删除成功!"<<endl;
return OK; }
Status SyList(SqList &A,SqList &B) { SqList C; InitList(C); sort(A.elem,A.elem+A.length+1); sort(B.elem,B.elem+B.length+1);
if(A.length+B.length>C.listsize) { while(1) { if(A.length+B.length>C.listsize) { C.elem=(ElemType *)realloc(C.elem,LISTINCREMENT * sizeof(ElemType)); C.listsize+=LISTINCREMENT; } } }
int k=1; int i=1,j=1;
while(1){ if(i>A.length){ for(int m=j;m<=B.length;m++) { C.elem[k]=B.elem[m]; k++; C.length++; } break; } if(j>B.length){ for(int m=i;m<=A.length;m++) { C.elem[k]=A.elem[m]; k++; C.length++; } break; } if(A.elem[i]>B.elem[j]){ C.elem[k]=B.elem[j]; k++; C.length++; j++; } else if(A.elem[i]==B.elem[j]){ C.elem[k]=A.elem[i]; k++; C.length++; C.elem[k]=B.elem[j]; k++; C.length++; i++; j++; } else{ C.elem[k]=A.elem[i]; k++; C.length++; i++; } }
cout<<"合成之后的表为:"; for(int i=1;i<=C.length;i++) { cout<<C.elem[i]<<" "; }
cout<<endl; return OK; }
Status PrintList(SqList &L) { cout<<"当前顺序表所含有的元素为:"; for(int i=1;i<=L.length;i++) { cout<<L.elem[i]<<" "; } cout<<endl; return OK; }
Status ListEmpty(SqList &L) { int pos;
if(L.length==0) { cout<<"未找到满足判断条件的数据元素!"<<endl; cout<<endl; return FALSE; } else { for(int i=0;i<=L.length;i++) { if(L.elem[i] != -1) { pos=i; break; } }
cout<<"顺序表中第一个满足条件的数据元素的位序为:"<<pos<<",其值为:"<<L.elem[pos]<<endl; cout<<endl; return OK; } }
int main() { while(1) { SqList L; SqList A; SqList B;
cout<<endl; cout<<"**1,创建一个顺序表,并对其赋值"<<endl; cout<<"**2,查询第一个满足判定条件的数据元素,若存在,则返回他的位序,否则返回0"<<endl; cout<<"**3,在第i个元素之前插入新的元素"<<endl; cout<<"**4,删除顺序表L的第i个元素,用e返回其值"<<endl; cout<<"**5,创建A和B两个顺序表,并将其按值递增合成"<<endl; cout<<"**6,打印顺序表"<<endl; cout<<"**0,退出系统"<<endl;
cout<<"请输入要进行的操作:"; int choice; cin>>choice; cout<<endl;
switch(choice) { case 1:InsertList(L);break; case 2:ListEmpty(L);break; case 3: { int pos,e; cout<<"请输入要插入的位置:"; cin>>pos; cout<<endl<<"请输入要插入的元素:"; cin>>e; cout<<endl; InsertValue(L,pos,e); break; } case 4: { int pos; cout<<"请输入要删除的位置:"; cin>>pos; cout<<endl; DeleteValue(L,pos); break; } case 5: { InitList(A); InitList(B); cout<<"A:"; InsertList(A); cout<<"B:"; InsertList(B); SyList(A,B); break; } case 6:PrintList(L);break; case 0:exit(0); } } system("pause"); return 0; }
|