c语言。大佬们,救救孩子吧,我快被这道题目逼疯了QAQ
题目:
已有a、b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。
第一行,a、b两个链表元素的数量N、M,用空格隔开。
接下来N行是a的数据
然后M行是b的数据
每行数据由学号和成绩两部分组成
输出要求:
按照学号升序排列的数据
我的代码:
#include<stdio.h>
#include<stdlib.h>
#define L sizeof(struct x)
struct x
{
int xue;
int fen;
struct x *next;
};
int n,m,i,k,c;
int main()
{
struct x*heada,*headb;
struct x *p1a,*p2a,*p1b,*p2b,*p,*h;
scanf("%d%d",&n,&m);
p1a=NULL;p2a=p1a;heada=p1a;
if(n>1)p1a=(struct x *)malloc(L),scanf("%d%d",&p1a->xue,&p1a->fen),p2a=p1a,heada=p1a;
for(i=0;i<n-1;i++)
{
p1a=(struct x *)malloc(L);
scanf("%d%d",&p1a->xue,&p1a->fen);
p2a->next=p1a;
p2a=p1a;
}
p1a->next=NULL;//input a
///////////////////////////////////////////////////
//////////////////////////////////////////////////
p1b=NULL;p2b=p1b;headb=p1b;
if(m>1)p1b=(struct x *)malloc(L),scanf("%d%d",&p1b->xue,&p1b->fen),p2b=p1b,headb=p1b;
for(i=0;i<m-1;i++)
{
p1b=(struct x *)malloc(L);
scanf("%d%d",&p1b->xue,&p1b->fen);
p2b->next=p1b;
p2b=p1b;
}
p1b->next=NULL;//input b
p1a->next=headb;
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
p=heada;
while(p->next!=NULL)
{
k=p->xue;h=p+1;
while(h->xue!=0)
{
if(k>(*h).xue)
{c=h->xue;h->xue=p->xue;p->xue=c;c=h->fen;h->fen=p->fen;p->fen=c;k=h->xue;}
h=h->next;
}
printf("%d %d\n",p->xue,p->fen);
p=p->next;
}
//////////////////////////////////////////////////////////////////这里排序就错掉了QAQ,程序运行后不输出东西
p=heada;
while(p->xue!=0)
{
printf("%d %d\n",p->xue,p->fen);
p=p->next;
}
return 0;
}
排序部分就错掉了,我不知道错在哪里以及要怎么改。谢谢大家了!QAQ