解题思路:


链式排序不是那么好弄。。所以用了交换


注意事项:





参考代码:

/*lb*/
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
    int num;
    int score;
    struct student *next;
}stu;
stu *create(stu *L,int n)
{
    int i;
    stu *p,*q;
    L = (stu *)malloc(sizeof(stu));
    q = L;
    L->next = NULL;
    for(i = 0; i < n; i++)//尾插
    {
        p = (stu *)malloc(sizeof(stu));
        p->next = NULL;
        scanf("%d%d",&p->num,&p->score);
        q->next = p;
        q = p;
    }
    return L;
}
stu *addandsort(stu *L1,stu *L2)
{
    stu *p,*q,*min;
    int t;
    p = L1;
    while(p->next!=NULL)
        p = p->next;
    p->next = L2->next;
    p = L1->next;
    while(p)
    {
        min = p;
        q = p->next;
        while(q)
        {
            if(min->num>q->num)
                min = q;
            q = q->next;
        }
        t = p->num;p->num = min->num;min->num = t;
        t = p->score;p->score = min->score;min->score = t;
        p = p->next;
    }
    return L1;
}
void print(stu *L)
{
    stu *p = L->next;
    while(p)
        printf("%d %d\n",p->num,p->score),p = p->next;
}
int main()
{
    stu *L1,*L2,*L3;
    int n,m;
    scanf("%d%d",&n,&m);
    L1 = create(L1,n);
    L2 = create(L2,m);
    L3 = addandsort(L1,L2);
    print(L3);
    return 0;
}


 

0.0分

5 人评分

  评论区

你的链表创建函数实际申请了n+1段空间,应该可以释放掉的
2019-10-12 23:00:32
  • «
  • 1
  • »