1 #include <cstdlib>
2 #include <iostream>
3 #define INIT_SIZE 100
4 #define LISTCREAMENT 10
5
6 using namespace std;
7 /*typedef struct student{
8 char name[20];
9 char number[20];
10 char class_[40];
11 char phone_number[40];
12 }student;*/
13 /*typedef struct{
14 student *elem;
15 int length;
16 int listsize;
17 }Sqliststudent;*/
18
19 typedef struct{
20 int *elem;
21 int length;
22 int listsize;
23 }Sqlist;
24
25
26
27 int initSqlist(Sqlist &L)//整型元素线性表的初始化;
28 {
29 L.elem=(int *)malloc(INIT_SIZE*sizeof(int));
30 if(L.elem==NULL)
31 {cout<<"存储分配失败\n";exit(2);}
32 L.listsize=INIT_SIZE;
33 L.length=0;
34 return 1;
35 }
36
37
38 /*int initSqliststudent(Sqliststudent &L)//student线性表的初始化;
39 {L.elem=(int *)malloc(INIT_SIZE*sizeof(student));
40 if(L.elem==NULL)
41 {cout<<"存储分配失败\n";exit(2);}
42 L.listsize=INIT_SIZE;
43 L.length=0;
44 return 1;
45 }*/
46
47 int input(Sqlist &L)//整型元素的输入;
48 {
49 cout<<"请输入整型数据,以^Z结束输入"<<endl;
50 int i,a;
51 i=0;
52 while(scanf("%d",&a)!=EOF)
53 {L.elem[i]=a;
54 i++;
55 L.length++;
56
57
58 if(L.length>=L.listsize)
59 {int *p;
60 p=NULL;
61 p=(int *)realloc(L.elem,sizeof(int)*(L.listsize+LISTCREAMENT));
62
63 if(p==NULL)
64 {cout<<"存储空间扩展失败,存储空间已满,不能再输入\n";
65 break;
66 }
67 L.elem=p;
68 L.listsize+=LISTCREAMENT;
69 }
70
71
72 }
73 cout<<"输入结束\n";
74 return 1;
75
76
77
78
79 }
80
81 /*int input(Sqliststudent &L)//student的输入
82 {student *p;
83 p=NULL;
84 cout<<"请输入学生的基本信息\n输入#结束输入\n";
85 while(1)
86 {
87 cout<<"姓名: ";
88 cin>>L.name;
89 if(strcmp(L.name,"#")==0)
90 {cout<<"输入结束\n";break;}
91 cout<<"学号: ";
92 cin>>L.number;
93 cout<<"专业班级: ";
94 cin>>L.class_;
95 cout<<"手机号: ";
96 cin>>L.phone_number;
97 L.length++;
98 if(L.length>=L.listsize)
99 p=(student *)realloc(L.elem,sizeof(student)*(L.listsize+LISTCREAMENT));
100 if(p==NULL)
101 {cout<<"存储空间扩展失败,存储空间已满,不能再输入\n";
102 break;
103 }
104 L.elem=p;
105
106
107 }
108 return 1;
109
110 }*/
111
112
113 int output(Sqlist L)//整型输出;
114 {int i;
115 if(L.length==0)
116 {cout<<"表是空表\n";return 0;}
117 for(i=0;i<L.length;i++)
118 {
119 cout<<L.elem[i]<<" ";
120 }
121 cout<<endl;
122 return 1;
123 }
124 /*int output(Sqliststudent L)//student输出;
125 {
126 int i;
127 if(L.length==0)
128 {cout<<"表是空表\n";return 0;}
129 for(i=0;i<L.length;i++)
130 {cout<<"姓名: "<<L.elem[i].name<<endl;
131 cout<<"学号: "<<L.elem[i].number<<endl;
132 cout<<"专业班级: "<<L.elem[i].class_<<endl;
133 cout<<"手机号: "<<L.elem[i].phone_number<<endl;
134 }
135 cout<<"输出结束\n";
136 return 1;
137 }*/
138
139
140 int check(int e,Sqlist L)//查找;
141 {
142 int i;
143 for(i=0;i<L.length;i++)
144 {if(e==L.elem[i])
145 return 1;
146 }
147 return 0;
148 }
149 int judge(Sqlist L)//判断是否对称;
150 {
151 int i,n;
152 n=L.length;
153 if(n==0)
154 {cout<<"表是空表\n";return -1;}
155 if(n==1)
156 return 1;
157 n/=2;
158 for(i=0;i<n;i++)
159 {
160 if(L.elem[L.length-i]!=L.elem[i])
161 break;
162 }
163 if(i<n)
164 return 0;
165 else
166 return 1;
167 }
168
169
170 /*int change(Sqlist &L)
171 {int i,j,k,t;
172 for(i=0;i<L.length-1;i++)
173 {
174 for(j=0;j<L.length-1-i;j++)
175 if(L.elem[j]%2==0)
176 k=L.elem[j],L.elem[j]=L.elem[j+1],L.elem[j+1]=k;
177 }
178 return 1;
179 }
180 */
181 int change(Sqlist &L)//奇偶对换;
182 {
183 int i,j,k,t;
184 for(i=0;i<L.length-1;i++)
185 {k=i;
186 if(L.elem[k]%2==1)
187 continue;
188 else if(L.elem[k]%2==0)
189 {
190 for(j=i+1;j<L.length;j++)
191 if(L.elem[j]%2==1)
192 {k=j;break;}
193 }
194 if(k!=i)
195 t=L.elem[i],L.elem[i]=L.elem[j],L.elem[j]=t;
196 }
197 return 1;
198 }
199
200 int insertsort(Sqlist &L)//有序插入;
201 {
202 initSqlist(L);
203 int i,j,k;
204 int a;
205 cout<<"请输入整型数据,以^Z结束输入\n";
206 i=0;
207 while(scanf("%d",&a)!=EOF)
208 {
209 /*if(L.length==0)
210 {L.elem[i]=a;L.length++;i++;}*/
211 //else
212 //{
213 //cout<<a<<endl;
214 for(j=0;j<L.length;j++)
215 if(a<L.elem[j])
216 break;
217
218 if(j<L.length)
219 { for(k=L.length-1;k>=j;k--)
220 L.elem[k+1]=L.elem[k];
221 L.elem[j]=a;
222 //i++;
223 L.length++;
224
225 }
226 else
227 {L.elem[L.length]=a;L.length++;}
228
229 //}
230 if(L.length>=L.listsize)
231 {int *p;
232 p=NULL;
233 p=(int *)realloc(L.elem,sizeof(int)*(LISTCREAMENT+L.listsize));
234
235
236 if(p==NULL)
237 {cout<<"存储空间扩展失败,存储空间已满,不能再输入\n";
238 break;
239 }
240 L.elem=p;
241 L.listsize+=LISTCREAMENT;
242 }
243
244
245 }
246
247 return 1;
248 }
249
250 int mergelist(Sqlist L1,Sqlist L2,Sqlist &L3)//合并;
251 {
252 //insertsort(L1);
253 //insertsort(L2);
254 initSqlist(L3);
255 int i,j,k;
256 if(L1.length+L2.length==0)
257 {cout<<"两个表均为空表,无法合并\n";return 0;}
258 if(L3.listsize<L1.length+L2.length)
259 {int *p;
260 p=NULL;
261 p=(int *)realloc(L3.elem,sizeof(int)*(L3.listsize+LISTCREAMENT));
262 if(p==NULL)
263 {cout<<"存储空间扩展失败,无法合并\n";return 0;}
264 L3.elem=p;
265 L3.listsize+=LISTCREAMENT;
266 }
267
268
269 i=j=0;k=0;
270 while((i<L1.length)&&(j<L2.length))
271 {if(L1.elem[i]<L2.elem[j])
272 {L3.elem[k]=L1.elem[i];k++;i++;}
273 else
274 {L3.elem[k]=L2.elem[j];k++;j++;}
275
276 }
277
278
279 while(i<L1.length)
280 {
281 L3.elem[k]=L1.elem[i];
282 i++;k++;
283 }
284 while(j<L2.length)
285 { L3.elem[k]=L2.elem[j];
286 j++;k++;
287 }
288 L3.length=L1.length+L2.length;
289
290 cout<<"合并成功\n";
291 return 1;
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319 int main(int argc, char *argv[])
320 {Sqlist L1,L2,L3,L;
321 int e;
322 initSqlist(L);
323 input(L);
324 output(L);
325 cout<<"请输入要查找的元素\n";
326 scanf("%d",&e);//cin>>e;
327 if(1==check(e,L))
328 cout<<"该元素存在\n";
329 else
330 cout<<"该元素不存在\n";
331 if(1==judge(L))
332 cout<<"该表对称\n";
333 else
334 cout<<"该表不对称\n";
335 cout<<"将表中奇数排在偶数之前\n";
336
337 change(L);
338 cout<<"排列后的元素\n";
339 output(L);
340 free(L.elem);
341
342 cout<<"建立有序表\n";
343
344 insertsort(L1);
345 output(L1);
346 free(L1.elem);
347 cout<<"建立有序表1\n";
348 insertsort(L1);
349 output(L1);
350
351 cout<<"建立有序表2\n";
352 insertsort(L2);
353 output(L2);
354 cout<<"将两表合并\n";
355 mergelist(L1,L2,L3);
356 cout<<"合并后的表为\n";
357 output(L3);
358 free(L1.elem);
359 free(L2.elem);
360 free(L3.elem);
361
362
363
364
365
366
367
368
369 system("PAUSE");
370 return EXIT_SUCCESS;
371 }
372
373