#include
#include<malloc.h>
using namespace std;
typedef struct LN
{
int data;
struct LN* next;
}LN, * LinkList;
void CreateList_L(LinkList& L, int n)
{
LinkList p, q = L;
L = (LinkList)malloc(sizeof(LN));
L->next = NULL;
while (n--)
{
p = (LinkList)malloc(sizeof(LN));
cin >> p->data;
q->next = p;
p->next = NULL;
q = p;
}
}
void MergeList(LinkList& L1, LinkList L2)
{
LinkList p, q, r;
r = L1;
p = L1->next;
q = L2->next;
while (p && q)
{
if (q->data <= p->data)
{
r->next = q;
r = r->next;
q = q->next;
}
else
{
r->next = p;
r = r->next;
p = p->next;
}
}
if (p)r->next = p;
if (q)r->next = q;
}
void output(LinkList L)
{
LinkList p;
p = L->next;
while (p)
{
cout << p->data << ' ';
p = p->next;
}
}
int main()
{
int x1, x2;
LinkList L1, L2;
cin >> x1 >> x2;
CreateList_L(L1, x1);
CreateList_L(L2, x2);
MergeList(L1, L2);
output(L1);
}
为什么运行后输入不了数据
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
CSDN专家-天际的海浪 2021-12-08 18:47关注q = L; // q要在L分配了空间后,再指向l你题目的解答代码如下:
#include<iostream> #include <malloc.h> using namespace std; typedef struct LN { int data; struct LN *next; } LN, *LinkList; void CreateList_L(LinkList &L, int n) { LinkList p, q; L = (LinkList)malloc(sizeof(LN)); q = L; // q要在L分配了空间后,再指向l L->next = NULL; while (n--) { p = (LinkList)malloc(sizeof(LN)); cin >> p->data; q->next = p; p->next = NULL; q = p; } } void MergeList(LinkList &L1, LinkList L2) { LinkList p, q, r; r = L1; p = L1->next; q = L2->next; while (p && q) { if (q->data <= p->data) { r->next = q; r = r->next; q = q->next; } else { r->next = p; r = r->next; p = p->next; } } if (p) r->next = p; if (q) r->next = q; } void output(LinkList L) { LinkList p; p = L->next; while (p) { cout << p->data << ' '; p = p->next; } } int main() { int x1, x2; LinkList L1, L2; cin >> x1 >> x2; CreateList_L(L1, x1); CreateList_L(L2, x2); MergeList(L1, L2); output(L1); }如有帮助,望采纳!谢谢!
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用