链表合并问题
#include<stdio.h>#include<malloc.h>
struct student
{
__int64 num;
char sex;
char name[10];
char from[20];
struct student *next;
};
struct student *creat()
{
struct student *head,*p,*q;
p=(struct student *)malloc(sizeof(struct student));
head=p;
q=head;head->next=NULL;
while(scanf("%I64d %c %s %s",&p->num,&p->sex,p->name,p->from)!=EOF)
{
if(head!=p)
{
q=head;
if(q->num>=p->num)
{
p->next=head;
head=p;
}
else
{
while(q->next!=NULL&&q->next->num<=p->num)
{
q=q->next;
}
p->next=q->next;
q->next=p;
}
}
p=(struct student *)malloc(sizeof(struct student));
}
return head;
}
void out(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%I64d %c %s %s\n",p->num,p->sex,p->name,p->from);
p=p->next;
}
}
int main()
{
struct student *head;
head=creat();
out(head);
return 0;
}
假如要合并以下的两组信息:
5
20100862152 m zhang fuzou
20100862153 f li shanghai
20100862154 m wu sanming
20100862155 f wang zhejian
20100862157 f chen zhangzhou
3
20100862151 m li hunan
20100862158 m fang zhejian
20100862156 f shen tianjin
请问如何在这个排序的代码的基础上编写?
或者给个思路,链表合并有固定模式吗,谢谢