#include
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode;
void CreateList(LNode *&L,int a[],int n)
{
LNode *p;
L=new LNode;
L->next=NULL;
while (n)
{
p=new LNode;
p->data=a[n-1];
p->next=L->next;
L->next=p;
n--;
}
}
void DispList(LNode *L)
{
LNode *p;
p=L->next;
while(p)
{cout<
cout<
pb=L2->next;
L1->next=NULL;
pc=L1;
while(pa&&pb)
{
if(pa->data
{pc->next=pa;pc=pa;pa=pa->next;}
else
{pc->next=pb;pc=pb;pb=pb->next;}
}
while(pa)
{pc->next=pa;pc=pa;pa=pa->next;}
while(pb)
{pc->next=pb;pc=pb;pb=pb->next;}
pc->next=NULL;
}
void ListReverse(LNode* &L)//similar to head fist insert method
{
LNode* p,*q;
p=L->next;
L->next=NULL;//separate head and first elem node
while(p) //add node one by one
{
q=p->next;
p->next=L->next;
L->next=p;
p=q;
}
}
void main()
{
LNode *L1,*L2;
ElemType a[10]={2,4,6,8};
ElemType b[10]={1,3,5,7};
CreateList(L1,a,4);
DispList(L1);
ListReverse(L1);
DispList(L1);
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章