这么写为什么不对啊 有希望改嘛 错在哪里了 初学者自学的 别提醒我30个字了
6条回答 默认 最新
- _GX_ 2022-10-04 20:43关注
#include <bits/stdc++.h> using namespace std; typedef int ElemType; typedef struct Link { ElemType Elem; struct Link *next; } link; link *CreatLink(int n) { link *head = (link *)malloc(sizeof(link)); head->next = NULL; //创立头节点 link *temp = head; //创立头指针指向头节点 while (n--) { link *a = (link *)malloc(sizeof(link)); scanf("%d", &a->Elem); a->next = NULL; temp->next = a; temp = a; } return head; } link *merge(link *l1, link *l2) { if (l1 == NULL) return l2; if (l2 == NULL) return l1; l1 = l1->next; l2 = l2->next; link *head = (link *)malloc(sizeof(link)); head->next = NULL; //新头节点 link *tail = head; //新头指针 while (l1 && l2) { if (l1->Elem <= l2->Elem) { tail->next = l1; l1 = l1->next; } else { tail->next = l2; l2 = l2->next; } tail = tail->next; } //循环操作 if (l1) tail->next = l1; if (l2) tail->next = l2; //最后必有一个原链表还剩下一串链表 return head; } int main() { link *l1, *l2, *l3; int n1, n2; cout << "输入A链表表长及元素\n"; scanf("%d", &n1); l1 = CreatLink(n1); cout << "输入B链表表长及元素\n"; scanf("%d", &n2); l2 = CreatLink(n2); l3 = merge(l1, l2); cout << "合并后的C表为:\n"; link *p = l3->next; while (p) { printf("%d ", p->Elem); p = p->next; } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用