我写了一个程序,是数据结构合并了两个线性表的,运行没错误,就是没输出合并结果,哪位高手帮忙改一下吧

2025-01-06 17:43:39
推荐回答(1个)
回答1:

#include "stdafx.h"
#include
using namespace std;
typedef struct liu
{
int data; struct liu*next;
}test,*LinkList;
liu *p,*pa,*pb,*pc,*La,*Lb,*Lc;
int m=sizeof(test);

void build(LinkList &L)
{ int i,j;
cout<<"how many lnode do you need?"<cin>>j;
L=(test*)malloc(m);
L->next=(LinkList)malloc(m);
p=L->next;
cout<<"enter these nodes:"<for(i=1;i{
cin>>p->data;
p->next=(test*)malloc(m);
p=p->next;
}
cin>>p->data;
p->next=NULL ;}

void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc)
{
pa=La->next; pb=Lb->next; Lc=pc=La;
while(pa&&pb)
{
if(pa->data <= pb->data)

{
pc->next=pa; pc=pa; pa=pa->next;
}

else
{
pc->next=pb; pc=pb; pb=pb->next;}
}
pc->next = pa?pa:pb ;
free(Lb);
}

void display(LinkList &L)
{p=L;
cout<<"the new list is:"<while (p->next!=NULL)
{
cout<next->data<p=p->next;
}
cout<data<}

void main()
{
build(La);
build(Lb);
MergeList_L(La,Lb,Lc);
display(Lc);
}