对于链表,可以先将两个链表排序,然后再将其枚举合成一个链表。
或者是先将一个链表接到另一个链表的尾部,然后将总链表排序。
1 #include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <malloc.h> 6 using namespace std; 7 struct Node{ 8 int date; 9 struct Node *next; 10 }; 11 void Sort(struct Node *head,int count){ 12 struct Node *p; 13 p=(struct Node *)malloc(sizeof(struct Node *)); 14 int temp; 15 for(int i=0;i<count-1;i++){ 16 for(p=head->next;p->next!=NULL;p=p->next){ 17 if(p->date>p->next->date){ 18 temp=p->date; 19 p->date=p->next->date; 20 p->next->date=temp; 21 } 22 } 23 } 24 } 25 26 void bond(struct Node *head1,struct Node *head2){ 27 struct Node *p; 28 p=head1; 29 while(p->next!=NULL){ 30 p=p->next; 31 } 32 p->next=head2->next; 33 } 34 35 void insert(struct Node *head,int count){ 36 struct Node *p; 37 while(count--){ 38 p=(struct Node *)malloc(sizeof(struct Node *)); 39 scanf("%d",&p->date); 40 p->next=head->next; 41 head->next=p; 42 } 43 } 44 45 void out(struct Node *head){ 46 struct Node *p; 47 p=(struct Node *)malloc(sizeof(struct Node *)); 48 p=head; 49 while(p->next!=NULL){ 50 printf("%d ",p->next->date); 51 p=p->next; 52 } 53 printf("\n"); 54 } 55 int main(){ 56 struct Node *head1,*head2; 57 int n,m; 58 head1=(struct Node *)malloc(sizeof(struct Node *)); 59 head2=(struct Node *)malloc(sizeof(struct Node *)); 60 scanf("%d%d",&n,&m); 61 head1->next=NULL; 62 head2->next=NULL; 63 insert(head1,n); 64 insert(head2,m); 65 bond(head1,head2); 66 Sort(head1,n+m); 67 out(head1); 68 return 0; 69 }
时间: 2024-10-17 19:23:17