#include<stdio.h> #include<stdlib.h> #include<algorithm> using namespace std; typedef struct Node { int id; int score; struct Node *next; }Linklist; Linklist *create(int n)//创建一个链表(没有插入) { Linklist *pre,*head; head=(Linklist*)malloc(sizeof(Linklist)); head->next=NULL; pre=head; for(int i=0;i<n;i++) { Linklist *p=(Linklist *)malloc(sizeof(Linklist)); scanf("%d %d",&p->id,&p->score); p->next=NULL; pre->next=p; pre=p; } return head; } void Print(Linklist *head) { Linklist *p=head; p=p->next; while(p!=NULL) { printf("%d %d\n",p->id,p->score); p=p->next; } } Linklist *merge(Linklist *head1,Linklist *head2) { //找到head1最后一个节点 Linklist *p=head1; while(p->next!=NULL) { p=p->next; } p->next=head2->next; free(head2); return head1; } void BubbleSort(Linklist *head) { Linklist *p,*q; for(p=head->next;p!=NULL;p=p->next) for(q=p;q!=NULL;q=q->next) { if(p->id>q->id) { swap(p->id,q->id); swap(p->score,q->score); } } } int main() { int n,m; scanf("%d %d",&n,&m); Linklist *head1=create(n); Linklist *head2=create(m); head1=merge(head1,head2); BubbleSort(head1); Print(head1); } |
Double click to view unformatted code.