数据结构之 线性表--整理音乐

整理音乐

Time Limit: 1000MS Memory limit: 65536K

题目描述


请用链表完成下面题目要求。
xiaobai 很喜欢音乐,几年来一直在收集好听的专辑。他有个习惯,每次在听完一首音乐后会给这首音乐打分,而且会隔一段时间给打好分的音乐排一个名次。今天 xiaobai 打开自己的音乐文件夹,发现有很多不同时期打过分的排好序的子音乐文件夹,他想把这些音乐放到一块,组成一个分数有序的序列。由于音乐文件很多,而文件里音乐的数目也是不确定的,怎么帮帮 xiaobai 完成这件工作呢?
   

输入

输入数据第一行为一个整数n(n<1000),代表文件夹的数量。接下来是n个文件夹的信息,每个文件夹信息的第一行是一个数字m,代表这个文件夹里有m首歌,后面m行每行一个歌曲名、分数,之间用空格分开。

输出

输出一行,为所有音乐组成的一个序列,音乐只输出名字。

如果音乐分数相同则按照音乐名字典序进行排序。

示例输入

3
4
aaa 60
aab 50
aac 40
aad 30
2
kkk 60
kkd 59
3
qow 70
qwe 60
qqw 20

示例输出

qow aaa kkk qwe kkd aab aac aad qqw

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <algorithm>

using namespace std;

struct node
{
    int data;
    string s;
    struct node *next;
};

struct node *creat(int n)
{
    struct node *head, *tail, *p;
    int i;
    head = new struct node;
    head->next = NULL;

    tail=head;
    for(i=0; i<n; i++)
    {
        p = new struct node;
        cin>>p->s;
        scanf("%d", &p->data );

        p->next = NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}

struct node *merge(struct node *h1, struct node *h2 )
{
    struct node *p1, *p2, *tail;

    p1 = h1->next;  //
    p2 = h2->next;  // 这两部写错了,调试了好半天, 坑啊~~~
    tail = h1;
    delete h2;

    while(p1 && p2)
    {
        if(p1->data > p2->data )
        {
            tail->next=p1;
            tail=p1;
            p1=p1->next;
        }
        else if(p1->data == p2->data )
        {
            if(p1->s > p2->s)
            {
                tail->next=p2;
                tail=p2;
                p2=p2->next;
            }
            else
            {
                tail->next=p1;
                tail=p1;
                p1=p1->next;
            }
        }
        else
        {
            tail->next=p2;
            tail=p2;
            p2=p2->next;
        }
    }
    if(p1)
    {
        tail->next=p1;
    }
    else
    {
        tail->next=p2;
    }
    return h1;
}

int main()
{
    int t;
    cin>>t;

    int n;
    int i, j;
    int len;

    struct node *head, *head2, *w, *head3;

    cin>>n;
    len=n;
    //head=new struct node;
    //head2=new struct node;

    head = creat(n);

    for(j=1; j<t; j++)
    {
        cin>>n;
        head2=creat(n);

        head3 = merge(head, head2);
        head = head3;

        len+=n;
    }

    w=head->next;
    for(i=0; i<len; i++)
    {
        if(i==0)
          cout<<w->s;
        else
          cout<<" "<<w->s ;
        w=w->next;
    }
    cout<<endl;
    return 0;
}

 

posted @ 2014-11-12 13:04  我喜欢旅行  阅读(307)  评论(0编辑  收藏  举报