描述:
假设有两个按元素值递增次序排列的线性表,均以单链表形式存储。请编写算法将这两个单链表归并为一个按元素值递减次序排列的单链表,并要求利用原来两个单链表的结点存放归并后的单链表。
输入:
先输入两个整数 m, n,分别代表两个递增单链表的长度。
输入两行整数,分别代表两个递增单链表的结点值。
输出:
输出一个排好序的递减的链表。
输入样例
4 4
1 2 5 7
3 4 6 8
输出样例
8 7 6 5 4 3 2 1
Code1:(这个看看就行了,当时头脑有点不清楚)
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *prior;
struct node *next;
}node;
int main(void )
{
node *a,*b,*c,*p,*tail;
int data,i,j,ii,jj,k,t;
a=b=c=NULL;
t=0;
scanf("%d %d",&i,&j);
ii=i,jj=j;
k=i+j;
while (i--)
{
p=(node *) malloc(sizeof (node));
scanf("%d",&p->data);
if(!a)
{
a=p;
p->prior=NULL;
}
else
{
tail->next=p;
p->prior=tail;
}
p->next=NULL;
tail=p;
}
tail->next=a;
a->prior=tail;
while (j--)
{
p=(node *) malloc(sizeof (node));
scanf("%d",&p->data);
if(!b)
{
b=p;
p->prior=NULL;
}
else
{
tail->next=p;
p->prior=tail;
}
p->next=NULL;
tail=p;
}
tail->next=b;
b->prior=tail;
c=tail=(node *)malloc(sizeof(node));
while (ii&&jj)
{
if(b->prior->data>a->prior->data)
{
tail->next=b->prior;
tail=b->prior;
b->prior=b->prior->prior;
jj--;
}
else if(b->prior->data<a->prior->data)
{
tail->next=a->prior;
tail=a->prior;
a->prior=a->prior->prior;
ii--;
}
else
{
tail->next=b->prior;
tail=b->prior;
b->prior=b->prior->prior;
a->prior=a->prior->prior;
jj--;
t++;
}
}
if(!jj)
{
tail->next=a->prior;
while(--ii)
{
tail=a->prior;
a=a->prior;
tail->next=a->prior;
}
}
else
{
tail->next=b->prior;
while(--jj)
{
tail=b->prior;
b=b->prior;
tail->next=b->prior;
}
}
p=c->next;
k-=t;
while(k--)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
Code2:(骗OJ版)
#include "stdio.h"
#include "stdlib.h"
#define maxsize 1000
int main(void )
{
int sizea,sizeb;
int *stacka,*stackb;
int posa,posb;
posa=posb=-1;
stacka=(int *) malloc(maxsize*sizeof (int ));
stackb=(int *) malloc(maxsize*sizeof (int ));
scanf("%d %d",&sizea,&sizeb);
while (++posa<sizea)
{
scanf("%d",&stacka[posa]);
}
while (++posb<sizeb)
{
scanf("%d",&stackb[posb]);
}
posa--,posb--;
while (posa!=-1&&posb!=-1)
{
if(stacka[posa]<stackb[posb])
{
printf("%d ",stackb[posb]);
posb--;
}
else if(stacka[posa]>stackb[posb])
{
printf("%d ",stacka[posa]);
posa--;
}
else
{
printf("%d ",stacka[posa]);
posa--;
posb--;
}
}
if(posa==-1)
{
while(posb!=-1)
{
printf("%d ",stackb[posb--]);
}
}
else
{
while(posa!=-1)
{
printf("%d ",stacka[posa--]);
}
}
free(stacka);
free(stackb);
return 0;
}
Code3:(单向链表,推荐)
#include <stdio.h>
#include <stdlib.h>
typedef struct prior_node
{
int data;
struct prior_node *prior;
}prior_node;
typedef struct next_node
{
int data;
struct next_node *next;
}next_node;
prior_node *Insert(prior_node *l,int times)
{
int i;
prior_node *tail,*temp;
for(i=1;i<=times;i++)
{
tail=(prior_node *) malloc(sizeof (prior_node));
scanf("%d",&tail->data);
if(!l)
{
l=tail;
l->prior=NULL;
}
else
{
tail->prior=temp;
}
temp=tail;
}
return tail;
}
int main(void )
{
int m,n,i;
prior_node *l,*tailm,*tailn;
next_node *lnew,*tail,*temp;
lnew=l=NULL;
scanf("%d%d",&m,&n);
tailm= Insert(l,m);
tailn= Insert(l,n);
while(tailm&&tailn)
{
temp=(next_node *) malloc(sizeof (next_node));
if(tailm->data>tailn->data)
{
temp->data=tailm->data;
tailm=tailm->prior;
}
else if(tailm->data<tailn->data)
{
temp->data=tailn->data;
tailn=tailn->prior;
}
else
{
temp->data=tailm->data;
tailm=tailm->prior;
tailn=tailn->prior;
}
if(!lnew)
{
lnew=temp;
}
else
{
tail->next=temp;
}
temp->next=NULL;
tail=temp;
}
tail->next=tailm?tailm:tailn;
while(lnew)
{
printf("%d ",lnew->data);
lnew=lnew->next;
}
return 0;
}