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
| #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define INITSIZE 100 #define INCREMENT 10
typedef int ElemType; typedef struct //顺序表结构体 { ElemType* elem; int length; int listsize; } SqList;
int InitSqList(SqList* L); void InputSqList(SqList* L); void PrintSqList(SqList L); int InsertSqList(SqList* L, int i, ElemType e); int DeleteSqList(SqList* L, int i, ElemType* e); int LocateSqList(SqList L, ElemType e); int ModifySqList(SqList* L, int i, ElemType e); void DuplicateRemoveSqList(SqList* L); void SortSqList(SqList* L); int MergeSqList(SqList LA, SqList LB, SqList* L); void test01(); void test02(); void test03(); void menu();
int main(void) { menu(); return 0; }
int InitSqList(SqList* L) { L->elem = (ElemType*)malloc(INITSIZE * sizeof(ElemType)); if (!L->elem) return 0; L->length = 0; L->listsize = INITSIZE; return 1; }
void InputSqList(SqList* L) { int i = 0, n = 0; printf("请输入该线性表的元素个数:n="); scanf("%d", &n); while (n > L->listsize) { printf("超过了线性表的初始存储空间,请重新输入:\n"); scanf("%d", &n); } L->length = n; printf("请依次输入该线性表各元素的值:"); for (i = 0; i < n; i++) scanf("%d", &L->elem[i]); }
void PrintSqList(SqList L) { int i = 0; printf("该线性表的元素依次为:"); for (i = 0; i < L.length; i++) printf("%d ", L.elem[i]); printf("\n"); }
int InsertSqList(SqList* L, int i, ElemType e) { int j = 0; if (i<1 || i>L->length + 1) return 0; if (L->length >= L->listsize) { ElemType* newbase = (ElemType*)realloc(L->elem, (L->listsize + INCREMENT) * sizeof(ElemType)); if (!newbase) return 0; L->elem = newbase; L->listsize += INCREMENT; } for (j = L->length - 1; j >= i - 1; j--) { L->elem[j + 1] = L->elem[j]; } L->elem[i - 1] = e; ++(L->length); return 1; }
int DeleteSqList(SqList* L, int i, ElemType* e) { int j = 0; if (i<1 || i>L->length) return 0; *e = L->elem[i - 1]; for (j = i; j < L->length; j++) { L->elem[j - 1] = L->elem[j]; } --(L->length); return 1; }
int LocateSqList(SqList L, ElemType e) { int i = 0; while (i < L.length && L.elem[i] != e) i++; if (L.elem[i] == e) return i + 1; else return 0; }
int ModifySqList(SqList* L, int i, ElemType e) { if (i<1 || i>L->length) return 0; L->elem[i - 1] = e; return 1; }
void DuplicateRemoveSqList(SqList* L) { int i = 0, j = 0; ElemType e; for (j = 1; j < L->length; j++) { for (i = 0; i < j; i++) { if (L->elem[i] == L->elem[j]) { DeleteSqList(L, i + 1, &e); j--; } } } }
void SortSqList(SqList* L) { int i = 0, j = 0; for (j = 0; j < L->length - 1; j++) { int flag = 0; for (i = 0; i < L->length - 1 - j; i++) { if (L->elem[i] > L->elem[i + 1]) { ElemType temp = L->elem[i]; L->elem[i] = L->elem[i + 1]; L->elem[i + 1] = temp; flag = 1; } } if (flag == 0) break; } }
int MergeSqList(SqList LA, SqList LB, SqList* L) { int i = 0, j = 0, k = 0; L->listsize = LA.length + LB.length; L->elem = (ElemType*)malloc(L->listsize * sizeof(ElemType)); if (!L->elem) return 0; while (i < LA.length && j < LB.length) { if (LA.elem[i] <= LB.elem[j]) L->elem[k++] = LA.elem[i++]; else L->elem[k++] = LB.elem[j++]; } while (i < LA.length) L->elem[k++] = LA.elem[i++]; while (j < LB.length) L->elem[k++] = LB.elem[j++]; L->length = k; return 1; }
void test01() { SqList L; int i = 0, status = 0; ElemType e; InitSqList(&L); printf("顺序表L初始化成功!\n"); InputSqList(&L); printf("输入待插入元素的值:"); scanf("%d", &e); printf("输入插入位置:"); scanf("%d", &i); status = InsertSqList(&L, i, e); if (status) PrintSqList(L); else printf("位置不合法,插入失败!\n"); printf("输入待删除元素的位置:"); scanf("%d", &i); status = DeleteSqList(&L, i, &e); if (status) { printf("删除元素的值为:%d\n删除后:", e); PrintSqList(L); } else printf("位置不合法,删除失败!\n"); printf("输入待查找元素的值:"); scanf("%d", &e); int position = LocateSqList(L, e); if (position == 0) printf("该顺序表中不存在该元素!\n"); else printf("该元素位置为:第%d个元素\n", position); printf("输入待修改元素的位置:"); scanf("%d", &i); printf("输入修改后的值:"); scanf("%d", &e); status = ModifySqList(&L, i, e); if (status) { printf("修改后,"); PrintSqList(L); } else printf("位置不合法,修改失败!\n"); system("pause"); }
void test02() { SqList L; InitSqList(&L); printf("顺序表L初始化成功!\n"); InputSqList(&L); PrintSqList(L); DuplicateRemoveSqList(&L); printf("去除重复元素后:"); PrintSqList(L); system("pause"); }
void test03() { SqList L1, L2, L; InitSqList(&L); InitSqList(&L1); printf("顺序表L1初始化成功!\n"); InputSqList(&L1); SortSqList(&L1); InitSqList(&L2); printf("顺序表L2初始化成功!\n"); InputSqList(&L2); SortSqList(&L2); printf("将L1和L2合并为新的递增顺序表L:\n"); MergeSqList(L1, L2, &L); PrintSqList(L); system("pause"); }
void menu() { system("cls"); int sel = 0; printf("1:建立顺序表,在顺序表上实现插入、删除、查找、修改、输出等操作\n"); printf("2:去除顺序表中的重复元素\n"); printf("3:对两个顺序表进行排序及合并操作\n"); printf("4:退出\n"); printf("输入你的选择:"); scanf("%d", &sel); while (sel < 1 || sel>4) { printf("输入的选择不合法,请重新输入:"); scanf("%d", &sel); } switch (sel) { case 1: test01(); menu(); break; case 2: test02(); menu(); break; case 3: test03(); menu(); break; case 4: exit(0); default: break; } system("pause"); }
|