#include<iostream>
using namespace std;
typedef struct Lnode{
int data;
struct Lnode *next;
}Lnode,*Linklist;
int initlist(Linklist &L)
{
L=new Lnode;
L->next=NULL;
return 1;
}
void creatlist(Linklist &L,int n)
{
int i=0;
Lnode *p;
L=new Lnode;
L->next=NULL;
for(i=0;i<n;++i)
{
p=new Lnode;
cin>>p->data;
p->next=L->next;
L->next=p;
}
}
int print(Linklist L)
{
Lnode *p;
p=L->next;
while(p!=0)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
void mergelist(Linklist &La,Linklist &Lb,Linklist &Lc)
{
Lnode *pa,*pb,*tmp;
pa=La->next;
pb=Lb->next;
Lc=La;
Lc->next=NULL;
while(pa&&pb)
{
if(pa->data<=pb->data)
{
tmp=pa->next;
pa->next=Lc->next;
Lc->next=pa;
pa=tmp;
}
else
{
tmp=pb->next;
pb->next=Lc->next;
Lc->next=pb;
pb=tmp;
}
}
while(pa)
{
tmp=pa->next;
pa->next=Lc->next;
Lc->next=pa;
pa=tmp;
}
while(pb)
{
tmp=pb->next;
pb->next=Lc->next;
Lc->next=pb;
pb=tmp;
}
delete Lb;
}
void mergelist_L(Linklist &La, Linklist &Lb, Linklist &Lc)
{
Lnode *pa,*pb,*pc;
pa=La->next;
pb=Lb->next;
Lc=La;
pc=Lc;
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;
delete Lb;
}
int max(Linklist L)
{
if(L->next==0){
cout<<"结果为空:";
}
Lnode *max,*p;
max=L->next;
p=L->next->next;
while(p)
{
if(p->data>max->data)
max=p;
p=p->next;
}
return max->data;
}
void reverse(Linklist &L)
{
Lnode *p;
p=L->next;
L->next=NULL;
while(p)
{
Lnode *q;
q=p->next;
p->next=L->next;
L->next=p;
p=q;
}
}
int main()
{
int a;
Linklist L1,L2,L3,L4,L5,L6;
initlist(L1);
initlist(L2);
creatlist(L1,4);
creatlist(L2,6);
cout<<"链表1:";
print(L1);
cout<<"链表2:";
print(L2);
mergelist(L1,L2,L3);
cout<<"非递增链表3:";
print(L3);
initlist(L4);
initlist(L5);
creatlist(L4,4);
creatlist(L5,6);
cout<<"链表4:";
print(L4);
cout<<"链表5:";
print(L5);
cout<<"非递减链表6:";
mergelist_L(L4,L5,L6);
print(L6);
initlist(L1);
initlist(L2);
creatlist(L1,4);
creatlist(L2,6);
a = max(L1);
cout<<"链表1中最大结点 :"<<a;
a = max(L2);
cout<<"链表2中最大结点 :"<<a;
initlist(L1);
creatlist(L1,4);
reverse(L1);
print(L1);
return 0;
}
为什么我将单链表翻转,得不到正确的翻转结果
求专家解答ლ(′◉❥◉`ლ)