本文汇总了C语言下实现及操作顺序表的方法,对于学习数据结构的朋友来说是一个不错的参考程序。完整代码如下:
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
|
#include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 typedef int status ; typedef int ElemType ; typedef struct { ElemType *elem; int length,listsize; }SqList; status InitList(SqList &L) //初始化 { L.elem=(ElemType *) malloc (LIST_INIT_SIZE* sizeof (ElemType)); if (!L.elem) exit (OVERFLOW); L.listsize=LIST_INIT_SIZE; L.length=0; return OK; } status Build(SqList &L) //建立表 { int i,n; printf ( "请输入元素个数n和n个元素\n" ); scanf ( "%d" ,&n); if (n>LIST_INIT_SIZE) //如果n大于当前空间 { L.elem=(ElemType *) realloc (L.elem,(n+LISTINCREMENT)* sizeof (ElemType)); if (!L.elem) exit (OVERFLOW); L.listsize=n+LISTINCREMENT; } for (i=0;i<n;i++) scanf ( "%d" ,L.elem+i); L.length=n; return OK; } void Print(SqList &L) //输出表中元素和长度 { int i; for (i=0;i<L.length;i++) printf ( "%d " ,*(L.elem+i)); printf ( "\n长度为:%d\n\n" ,L.length); } void Tips() //提示函数 { printf ( "请选择你的想要的操作:\n" ); printf ( "<1> 输出顺序表及顺序表的长度\n" ); printf ( "<2> 删除值为x的结点\n" ); printf ( "<3> 删除给定位置i的结点\n" ); printf ( "<4> 将顺序表逆置\n" ); printf ( "<5> 将顺序表按升序排序\n" ); printf ( "<6> 将x插入到顺序表的适当位置上\n" ); printf ( "<7> 将两个有序表合并\n" ); printf ( "<0> 退出\n\n" ); } status ListDelete1(SqList &L, int x) //删除值为X的元素 { int i; for (i=0;i<L.length;i++) if (*(L.elem+i)==x) break ; if (i==L.length) return ERROR; for (i++;i<L.length;i++) *(L.elem+i-1)=*(L.elem+i); L.length--; return OK; } status ListDelete2(SqList &L, int x) //删除第X个元素 { int i; if (x<0||x>=L.length) return ERROR; for (i=x+1;i<L.length;i++) *(L.elem+i-1)=*(L.elem+i); L.length--; return OK; } void Inverse(SqList &L) //逆置函数 { int i,t; for (i=0;i<L.length/2;i++) { t=*(L.elem+i); *(L.elem+i)=*(L.elem+L.length-i-1); *(L.elem+L.length-i-1)=t; } } void Sort(SqList &L) //冒泡排序(升序) { int i,j,t; for (i=1;i<L.length;i++) for (j=0;j<L.length-i;j++) { if (*(L.elem+j)>*(L.elem+j+1)) { t=*(L.elem+j); *(L.elem+j)=*(L.elem+j+1); *(L.elem+j+1)=t; } } printf ( "已按升序排列\n\n" ); } status ListInsert(SqList &L, int x) //将X插入,使仍然有序 { int i,k; if (L.length>=L.listsize) { L.elem=(ElemType *) realloc (L.elem,(L.listsize+LISTINCREMENT)* sizeof (ElemType)); if (!L.elem) exit (OVERFLOW); L.listsize+=LISTINCREMENT; } for (i=0;i<L.length;i++) if (x<*(L.elem+i)) break ; k=i; for (i=L.length;i>k;i--) *(L.elem+i)=*(L.elem+i-1); *(L.elem+k)=x; L.length++; return OK; } status Merger(SqList &L,SqList &Lb) //合并两个线性表 { int i,j,k; SqList Lc; InitList(Lc); if (Lc.listsize<L.length+Lb.length) { Lc.elem=(ElemType *) realloc (Lc.elem,(L.length+Lb.length+LISTINCREMENT)* sizeof (ElemType)); if (!L.elem) exit (OVERFLOW); Lc.listsize=L.length+Lb.length+LISTINCREMENT; } i=j=k=0; while (i<L.length && j<Lb.length) { if (*(L.elem+i) < *(Lb.elem+j)) { *(Lc.elem+k)=*(L.elem+i); k++;i++; } else { *(Lc.elem+k)=*(Lb.elem+j); k++;j++; } } while (i<L.length) { *(Lc.elem+k)=*(L.elem+i); k++;i++; } while (j<Lb.length) { *(Lc.elem+k)=*(Lb.elem+j); k++;j++; } Lc.length=L.length+Lb.length; L=Lc; return OK; } int main() { int op,x,flag; SqList L,Lb; InitList(L); Build(L); Tips(); scanf ( "%d" ,&op); while (op) { switch (op) { case 1: Print(L); break ; case 2: printf ( "请输入要删除的数据X:\n" ); scanf ( "%d" ,&x); flag=ListDelete1(L,x); if (flag) printf ( "删除成功!!\n\n" ); else printf ( "元素不存在,删除失败!!\n\n" ); break ; case 3: printf ( "请输入要删除的位置i:\n" ); scanf ( "%d" ,&x); flag=ListDelete2(L,x-1); //第i个元素对应的下标为i-1 if (flag) printf ( "删除成功!!\n\n" ); else printf ( "元素不存在,删除失败!!\n\n" ); break ; case 4: Inverse(L); break ; case 5: Sort(L); break ; case 6: printf ( "请输入要插入的数据X:\n" ); scanf ( "%d" ,&x); flag=ListInsert(L,x); if (flag) printf ( "插入成功!!\n\n" ); else printf ( "插入失败!!\n\n" ); break ; case 7: printf ( "请输入Lb的内容:\n" ); InitList(Lb); Build(Lb); flag=Merger(L,Lb); if (flag) printf ( "合并成功!!\n\n" ); break ; } Tips(); scanf ( "%d" ,&op); } return 0; } |