# 链表合并

``````2 3
5 100
6 89
3 82
4 95
2 10``````

``````2 10
3 82
4 95
5 100
6 89``````

``````#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int num;
float score;
struct student *next;
}stu;
stu *creat(int n)
{
int i;
for (i = 0; i < n; i++)
{
p = (stu *)malloc(sizeof(stu));
scanf("%d%f",&p->num,&p->score);
p->next = NULL;
if ( p->num <= 0)
{
free(p);
break;
}
else
tail->next = p;
tail = p;
}
}

void print(stu *p)
{
while (p != NULL)
{
printf("%d %.0f\n",p->num,p->score);
p = p->next;
}
}

stu *link(stu *p1,stu *p2)
{
head = (stu *)malloc(sizeof(stu));
{
q = (stu *)malloc(sizeof(stu));
q->num=p1->num;
q->score=p1->score;
q->next=p->next;
p->next=q;
p1=p1->next;
}
while(p1!=NULL)
{
q = (stu *)malloc(sizeof(stu));
q->num=p1->num;
q->score=p1->score;
while(p->next!=NULL)
{
if(p->next->num<q->num)
{
p=p->next;
}
else
{
q->next=p->next;
p->next=q;
break;
}
}
if(p->next==NULL)
{
q->next=p->next;
p->next=q;
}
p1=p1->next;

}
while(p2!=NULL)
{
q = (stu *)malloc(sizeof(stu));
q->num=p2->num;
q->score=p2->score;
while(p->next!=NULL)
{
if(p->next->num<q->num)
{
p=p->next;
}
else
{
q->next=p->next;
p->next=q;
break;
}
}
if(p->next==NULL)
{
q->next=p->next;
p->next=q;
}
p2=p2->next;
}
}

int main()
{
int n,m;
scanf("%d %d",&n,&m);
stu *a,*b,*c;
a = creat(n);
b = creat(m);
print(c);
return 0;
}
``````

``````#include <stdio.h>
#include <malloc.h>
{
int no;
int score;
};
/*by lyh   2013.9.26  */
{ /*递归建立 具有num节点的链表，返回链表的首节点 */
if(num>0)
{
scanf("%d%d",&p1->no,&p1->score);
p1->next=NULL;
if(p==NULL)
{
}
while(p->next)
p=p->next;
p->next = p1;

}
}
{
while(p)
{
printf("%d %d\n",p->no,p->score);
p=p->next;
}
}
{ /* 合并 */
while(p1->next)
p1=p1->next;
p1->next = p2;
}

while(p){
p1=p;
p=p->next;
free(p1);
}
}
{/*节点p2 插入到有序链表p1中*/
if(p1==NULL)
return p2;
while(p1&&p1->no<p2->no){
pre=p1;
p1=p1->next;
}
if(p1==NULL){ /*p2 插入在尾节点*/
pre->next=p2;
}
p2->next = p1;
return p2;
}
pre->next = p2;
p2->next = p1;
}
/* 对链表 p 实现插入排序 */
while(p){
p1=p;
p=p->next;
p1->next =NULL; /*将节点p从链表上摘下*/
}
}
int main()
{
int num1,num2;
scanf("%d%d",&num1,&num2);