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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
| #include <stdio.h> #include <stdlib.h>
typedef struct List { double element; List* next; }List;
List* ListInit() { List* head; head = (List*)malloc(sizeof(List)); if (head == NULL) { printf("开辟空间失败\n"); exit(0); } head->next = NULL; head->element = NULL;
return head; };
void CreatList(List* head) { int choice = 1; List* p, * last;
last = head; while (last->next != NULL) { last = last->next; }
while (choice == 1) { p = (List*)malloc(sizeof(List)); if (p == NULL) { printf("开辟空间失败\n"); exit(0); } printf("元素:"); scanf_s("%lf", &(p->element)); p->next = NULL; last->next = p; last = p; printf("continue?1-0 "); scanf_s("%d", &choice); } }
int EmptyList(List* head) { if (head->next != NULL) { return 1; } return 0; }
void OutputList(List* head) { if (EmptyList(head) == 0) { printf("链表为空\n"); } else { List* p; p = head->next; while (p != NULL) { printf("%lf", p->element); if (p->next != NULL) { printf(" -> "); } p = p->next; } printf("\n"); } }
int LongList(List* head) { int longlist = 0; List* p; p = head->next; while (p != NULL) { p = p->next; longlist++; } return longlist; }
int FindElement(List* head, double a) { List* p; p = head->next; int order = 1; while (p != NULL) { if (p->element == a) { printf("%lf 存在于第 %d 个节点里\n", p->element, order); break; } else { p = p->next; order++; } } if (order > LongList(head)) { order = -1; printf("不存在值为%lf的节点\n", a); } return order; }
void DeleteElement(List* head, double a) { List* p, * q; p = head; int mark = 0; while (p->next != NULL) { if (p->next->element == a) { q = p->next; p->next = q->next; free(q); mark = 1; break; } else { p = p->next; } } if (mark == 1) { printf("%lf 已被删除\n", a); } else { printf("无法找到元素 %lf\n", a); } }
void ClearList(List* head) { List* q, * p; q = head->next; while (q != NULL) { p = q; q = q->next; p->next = NULL; free(p); } head->next = NULL; printf("链表已被删除\n"); }
void DeleteThElement(List* head, int order) { int l = LongList(head); if (order < 1 || l < order) { printf("不存在序号为%d的节点\n", l); } else { List* p, * q; p = head; int mark = 0; while (p->next != NULL) { if (mark == order - 1) { q = p->next; p->next = q->next; free(q); printf("第%d个节点已被删除\n", order); break; } else { p = p->next; mark++; } } } }
void InsertList(List* head, int order, double data) { if (order < 0) { printf("不存在负序号的元素\n"); } else if (int l = LongList(head) < order) { printf("不存在序号为 %d 的元素\n", order); } else { List* p, * q; q = (List*)malloc(sizeof(List)); if (q == NULL) { printf("开辟空间失败\n"); exit(0); } q->element = data;
int mark = 0; p = head; while (mark != order) { mark++; p = p->next; } q->next = p->next; p->next = q; } }
double FindThElement(List* head, int order) { if (int l = LongList(head) < order || order < 1) { printf("不存在序号为%d的元素\n", order); return -9999; } else { List* p; int mark = 0; p = head; while (mark != order) { p = p->next; mark++; } return p->element; } }
void SwapElememt(List* head, int order1, int order2) { int l = LongList(head); if (order1 < 1 || order2 < 1) { printf("不存在非正序号的元素\n"); } else if (order1 > l || order2 > l) { printf("不存在序号超过 %d 的元素\n", l); } else { int min = order2, max = order1, mark = 0; double t; if (order1 < order2) { min = order1; max = order2; } List* p, * q; q = head; while (mark != min) { mark++; q = q->next; } p = q; while (mark != max) { mark++; q = q->next; } t = p->element; p->element = q->element; q->element = t; } }
int greater(double a, double b) { if (a > b) { return 1; } else { return -1; } }
int smaller(double a, double b) { if (a > b) { return -1; } else { return 1; } }
int FindBig_Small(List* head, int (*compare)(double, double)) { if (EmptyList(head) == 0) { printf("空链表无最大元素\n"); return 0; } else { List* p, * q; int mark = 1, flag = 1; p = head->next; double big_small; big_small = p->element; while (p->next != NULL) { p = p->next; flag++; if (compare(p->element, big_small) > 0) { big_small = p->element; mark = flag; } } return mark; } }
int In_DecrementList(List* head, int (*compare)(double, double)) { int flag = 1; List* p; p = head->next; while (p != NULL) { if (p->next != NULL && compare(p->element, p->next->element) > 0) { flag = 0; break; } else { p = p->next; } } return flag; }
void SortList(List* head, int (*compare)(double, double)) { List* p; p = ListInit(); int order = 0; int flag; while (EmptyList(head) == 1) { flag = FindBig_Small(head, compare); InsertList(p, order, FindThElement(head, flag)); order++; DeleteThElement(head, flag); } head->next = p->next; p->next = NULL; free(p); }
void ConverseList(List* head) { List* p, * q; p = head->next; q = ListInit(); while (p != NULL) { head->next = head->next->next; p->next = q->next; q->next = p; p = head->next; } head->next = q->next; free(q); }
List* MergeList(List* La, List* Lb, int (*compare)(double, double)) { SortList(La, compare); SortList(Lb, compare); List* p, * q, * t; p = La; q = Lb->next; while (q != NULL) { while (compare(q->element, p->next->element) < 0 && p->next != NULL) { p = p->next; } t = q; q = q->next; t->next = p->next; p->next = t; } Lb->next = NULL; ClearList(Lb); free(Lb); return La; }
void FunctionList(List* head) { int choice = 1; double data; int order, order1; while (choice != 0) { printf("链表为:\n"); OutputList(head);
printf("输入想实现的功能:\n"); printf("1 - 输出链表的长度\n"); printf("2 - 返回值为a的首个节点的位置\n"); printf("3 - 删除值为a的首个节点\n"); printf("4 - 删除第i个节点\n"); printf("5 - 在第i个位置插入数据\n"); printf("6 - 返回第i个位置的元素值\n"); printf("7 - 交换链表第n个节点与第m个节点的位置\n"); printf("8 - 寻找链表最大或最小的元素的位置\n"); printf("9 - 链表从大到小排序\n"); printf("10 - 链表从小到大排序\n"); printf("11 - 链表逆置\n"); printf("12 - 从大到小合并两个链表\n"); printf("13 - 从小到大合并两个链表\n"); printf("其他 - 退出\n");
printf("选择:"); scanf_s("%d", &choice);
if (choice == 1) { printf("链表的长度为:%d\n", LongList(head)); } else if (choice == 2) { printf("查找的值为:"); scanf_s("%lf", &data); printf("%lf 的位置为 %d\n", data, FindElement(head, data)); } else if (choice == 3) { printf("删除的值为:"); scanf_s("%lf", &data); DeleteElement(head, data); } else if (choice == 4) { printf("删除的位置为:"); scanf_s("%d", &order); DeleteThElement(head, order); } else if (choice == 5) { printf("插入的位置为:"); scanf_s("%d", &order); printf("插入的元素为:"); scanf_s("%lf", &data); InsertList(head, order, data); } else if (choice == 6) { printf("查找的位置为:"); scanf_s("%d", &order); printf("链表第 %d 个的值为 %lf \n", order, FindThElement(head, order)); } else if (choice == 7) { printf("交换节点的位置为:"); scanf_s("%d", &order); scanf_s("%d", &order1); SwapElememt(head, order, order1); } else if (choice == 8) { printf("最大元素的位置为:%d\n", FindBig_Small(head, greater)); printf("最小元素的位置为:%d\n", FindBig_Small(head, smaller)); } else if (choice == 9) { SortList(head, greater); } else if (choice == 10) { SortList(head, smaller); } else if (choice == 11) { ConverseList(head); printf("链表已逆置\n"); } else if (choice == 12) { printf("创建需要合并的链表\n"); List* L; L = ListInit(); CreatList(L); head = MergeList(head, L, greater); printf("链表已合并\n"); } else if (choice == 13) { printf("创建需要合并的链表\n"); List* L; L = ListInit(); CreatList(L); head = MergeList(head, L, smaller); printf("链表已合并\n"); } else { break; printf("退出\n"); } } }
int main() { List* head; head = ListInit(); CreatList(head); FunctionList(head); free(head);
return 0; }
|