您的位置:首页 > 编程语言 > C语言/C++

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

2013-05-11 21:18 357 查看


#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言