这么写为什么不对啊 有希望改嘛 错在哪里了 初学者自学的 别提醒我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无用
悬赏问题
- ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
- ¥15 gdf格式的脑电数据如何处理matlab
- ¥20 重新写的代码替换了之后运行hbuliderx就这样了
- ¥100 监控抖音用户作品更新可以微信公众号提醒
- ¥15 UE5 如何可以不渲染HDRIBackdrop背景
- ¥70 2048小游戏毕设项目
- ¥20 mysql架构,按照姓名分表
- ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
- ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
- ¥15 linux驱动,linux应用,多线程