将两个单链表 合并 .有序和无序 两种代码//
谢谢....
有序的我有~~
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define R 100
typedef int DataType;
struct Node;
typedef struct Node*PNode,*LinkList;
struct Node
{ DataType info;
PNode link;
};
LinkList createNullList_link(void)
{ LinkList llist;
llist=(LinkList)malloc(sizeof(struct Node));
if(llist!=NULL)
{llist->link=NULL;
llist->info=100;
}
return(llist);
}
int insert_link(LinkList llist,int i,DataType x)
{ PNode p,q;int j=0;
p=llist;
while(p!=NULL && j<i)
{p=p->link;j++;}
if(j!=i)
{printf("\nThe value of i=%d is not reasonable.\n",i);return(0);}
q=(PNode)malloc(sizeof(struct Node));
if(q==NULL)
{printf("\nOut of space!!\n");return(0);}
else
{q->info=x;q->link=p->link;p->link=q;return(1);}
}
int isNullList_link(LinkList llist)
{
return(llist->link==NULL);
}
int paixu_link(LinkList llist)
{
int j,fuzu;
PNode pi,pj,q;
LinkList pl=llist;
pi=pl->link;
while(pi->link!=NULL)
{
j=pi->info;
q=pi;
pj=pi->link;
while(pj!=NULL)
{
if(pj->info<j);
pj=pj->link;
}
if(q!=pi);
{
fuzu=pi->info;
pi->info=q->info;
q->info=fuzu;
}
pi=pi->link;
}
return(1);
}
int print_link(LinkList llist)
{
PNode p;
if(isNullList_link(llist))
{
printf("\n\nThis is a NULL Link\n\n");
return(0);
}
p=llist->link;
printf("\n\n");
while(p!=NULL)
{
printf("%d\t",p->info);
p=p->link;
}
return(1);
}
int hebing_l(LinkList plA,LinkList plB,LinkList plC)
{
LinkList pA,pB,pC;
pA=plA->link;
pB=plB->link;
plC=pC=plA;
while(pA!=NULL && pB!=NULL)
{
if(pA->info<=pB->info)
{
pC->link=pA;
pC=pA;
pA=pA->link;
}
else
{
pC->link=pB;
pC=pB;
pB=pB->link;
}
}
pC->link=pA ? pA:pB;
free(plB);
return(1);}
main()
{
int i,f,n;
LinkList plA,plB,plC;
randomize();
clrscr();
printf("\n\nShu zu A B de biao chang\n\n");
scanf("%d,%d",&f,&n);
plA=createNullList_link();
for(i=0;i<f;i++)
insert_link(plA,0,random(R));
paixu_link(plA);
print_link(plA);
plB=createNullList_link();
for(i=0;i<n;i++)
insert_link(plB,0,random(R));
paixu_link(plB);
print_link(plB);
hebing_l(plA,plB,plC);
printf("\n\nhe bing hou\n\n");
paixu_link(plA);
print_link(plA);
}