#include#include#define?ElemType?int
#define?Status?int
#define?OK?1
#define?FALSE?0
typedef?struct?LNode{
ElemType?data;
struct?LNode?*next;
}LNode,*LinkList;
//創(chuàng)建一個容量為size的單鏈表
Status?Create_List(LinkList?l,int?size)
{
int?i=0;
if(!l)
return?FALSE;
l->next?=NULL;
while(idata=0;
p->next?=?l->next;
l->next?=?p;
i++;
}
return?OK;
}
//遍歷輸出單鏈表的所有元素
Status?OutPut_List(LinkList?l)
{
LinkList?p;
if(l?==NULL)
{
printf("no?noden");
}
p=l->next;?
printf("輸出所有節(jié)點(diǎn)的數(shù)據(jù)n");
while(p!=NULL)
{
printf("%d?",p->data);
p?=?p->next;
}
printf("n");
return?OK;
}
//輸入單鏈表的節(jié)點(diǎn)元素size個
Status?Input_List(LinkList?l,int?size)
{
LinkList?p;
int?i=0;
p?=?l->next;
if(size?<=0)
{
printf("error?sizen");
return?FALSE;
}
printf("輸入所有節(jié)點(diǎn)的數(shù)據(jù)n");
while(idata);
p?=?p->next;
i++;
}
return?OK;
}
//在指定位置i處插入一個節(jié)點(diǎn)數(shù)據(jù)
Status?Insert_List(LinkList?l,int?i,ElemType?e)
{
LinkList?p;
int?j=1;
p=l->next;
if(i<0)
{
printf("Error?in");
return?FALSE;
}
while(j?<?i)
{
p?=?p->next;
j++;
}
LinkList?q=(LinkList)malloc(sizeof(LNode));
q->next?=?p->next;
p->next?=?q;
q->data?=?e;
return?OK;
}
//在指定位置i處刪除節(jié)點(diǎn)
Status?Delete_List(LinkList?l,int?i)
{
LinkList?p,q;
int?j=1;
p=?l;
if(i<0)
{
printf("Error?in");
return?FALSE;
}
while(j?<?i)
{
p?=p->next;
j++;
}
q?=?p->next;
p->next?=?q->next;
free(q);
return?OK;
}
//清空單鏈表所有的節(jié)點(diǎn)
Status?Clear_List(LinkList?l)
{
LinkList?p,q;
p=l->next;
while(p)
{
q?=?p->next;
free(p);
p?=?q;
}
l?->next?=?NULL;
return?OK;
}
//返回單鏈表的長度
Status?Length_List(LinkList?l)
{
LinkList?q;
int?i=0;
q=l->next;
while(q!=NULL)
{
i++;
q?=?q->next;
}
printf("length?is?%dn",i);
return?i;
}
//合并2個單鏈表(升序)
Status?Merge_List(LinkList?la,LinkList?lb,LinkList?lc)
{
LinkList?pa,pb,pc;
pa?=?la->next;
pb?=?lb->next;
pc?=?lc;
pc->next?=NULL;
while(pa?&&?pb)
{
if(pa->data?>=?pb->data)
{
pc->next?=?pb;
pc?=?pb;
pb?=?pb->next;
}
else
{
pc->next?=?pa;
pc?=?pa;
pa?=?pa->next;
}
}
pc->next?=pa?pa:pb;
return?OK;
}
int?main(void)
{
LinkList?la?=?(LinkList)malloc(sizeof(LNode));
LinkList?lb?=?(LinkList)malloc(sizeof(LNode));
LinkList?lc?=?(LinkList)malloc(sizeof(LNode));
int?size=0;
int?i=0,e=1111,d=0;
printf("input?sizen");
scanf("%d",&size);
Create_List(la,size);
Input_List(la,size);
Create_List(lb,size);
Input_List(lb,size);
//Create_List(lc,6);
Merge_List(la,lb,lc);
OutPut_List(lc);
return?0;
}