sdut.acm 2012级《程序设计基础Ⅱ)》_链表 整理音乐

#include <stdio.h>
#include <malloc.h>
#include <string.h>

struct node
{
    char name[10];
    int s;
    struct node *next;
};
struct node *create1(int num)
{
    struct node *head,*tail,*p;
    int i;
    head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    tail = head;
    for(i = 1;i <= num;i++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        p->next = NULL;
        scanf("%s %d",p->name,&p->s);
        tail->next = p;
        tail = p;
    }
    return (head);

}
struct node *merg(struct node *head1,struct node *head2)
{
    struct node *tail,*p1,*p2;
    p1 = head1->next;
    p2 = head2->next;
    tail = head1;
    free(head2);
    while(p1&&p2)
    {
        if(p1->s >= p2->s)
        {
            if(strcmp(p1->name,p2->name) < 0)
             {   tail->next  = p1;
                 tail = p1;
                 p1 = p1->next;
             }
             else
                {
                    tail->next = p2;
                    tail = p2;
                    p2 = p2->next;
                }
        }else
        {
            tail->next = p2;
            tail = p2;
            p2 = p2->next;
        }

    }
    if(p1)
        tail->next = p1;
    else
        tail->next = p2;

    return (head1);
}
 void output(struct node *head)
 {

     struct node *p;
     p = head->next;
     while(p->next != NULL)
     {
         printf("%s ",p->name);
         p = p->next;

    }
    printf("%s\n",p->name);
    free(p);
 }
int main()
{
    struct node *Head1,*Head2;
    int NUM,j,Num;
    scanf("%d",&NUM);
    scanf("%d",&Num);
    Head1 = create1(Num);
    if(NUM == 1)
    {
        output(Head1);

    }else
    {
          for(j = 2;j <= NUM;j++)
       {
           scanf("%d",&Num);
           Head2 = create1(Num);
           Head1 = merg(Head1,Head2);
       }
    }
      output(Head1);
      free(Head1);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值