#include<stdio.h>
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;
}Linklist;
void init(Linklist *&head)//链表的初始化
{
head = (Linklist *)malloc(sizeof(Linklist));
head->next= NULL;
}
void createlist(Linklist *&head,int n,int a[])//链表的增加
{
Linklist *r,*s;
r= head;
for(int i= 0 ;i<n;i++)
{
s= (Linklist *)malloc(sizeof(Linklist));
s->data = a[i];
r ->next = s;
r= s;
}
r->next=NULL;
}
void mergelist(Linklist *&head1,Linklist *&head2) //链表合并
{
Linklist *r1,*r2,*r3;
r1= head1->next;
r2= head2->next;
r3 =head1;
while(r1!=NULL&&r2!=NULL)
{
if(r1->data<=r2->data)
{
r3->next= r1;
r3=r1;
r1 = r1->next;
}
else
{
r3->next=r2;
r3= r2;
r2 = r2->next;
}
}
if(r1==NULL)
{
while(r2!=NULL){
r3->next = r2;
r3= r2;
r2=r2->next;}
}
else{
while(r1!=NULL){
r3->next =r1;
r3 = r1;
r1 = r1->next;}
}
}
void display(Linklist *&head)//链表的展示
{
Linklist *r;
r=head->next;
while(r!=NULL)
{
cout<<r->data<<" ";
r=r->next;
}
cout<<endl;
}
void sortlist(Linklist *&head)//选择排序
{
Linklist *p,*q,*small;
int temp;
for(p = head->next; p->next!=NULL;p=p->next)
{
small = p;
for(q=p->next;q;q=q->next)
{
if(q->data<small->data)
{
small = q;
}
}
if(small!=p)
{
temp = p->data;
p->data =small->data;
small->data = temp;
}
}
}
int main()
{
Linklist *head,*head1,*head2;
init(*&head1);
init(*&head);
init(*&head2);
int n1;int n2;
int a[30],b[30];
cin>>n1>>n2;
for(int i = 0;i<n1;i++)
cin>>a[i];
for(int i = 0;i<n2;i++)
cin>>b[i];
createlist(*&head1,n1,a);
createlist(*&head2,n2,b);
mergelist(*&head1,*&head2);
display(*&head1);
sortlist(*&head1);
display(*&head1);
return 0;
}
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;
}Linklist;
void init(Linklist *&head)//链表的初始化
{
head = (Linklist *)malloc(sizeof(Linklist));
head->next= NULL;
}
void createlist(Linklist *&head,int n,int a[])//链表的增加
{
Linklist *r,*s;
r= head;
for(int i= 0 ;i<n;i++)
{
s= (Linklist *)malloc(sizeof(Linklist));
s->data = a[i];
r ->next = s;
r= s;
}
r->next=NULL;
}
void mergelist(Linklist *&head1,Linklist *&head2) //链表合并
{
Linklist *r1,*r2,*r3;
r1= head1->next;
r2= head2->next;
r3 =head1;
while(r1!=NULL&&r2!=NULL)
{
if(r1->data<=r2->data)
{
r3->next= r1;
r3=r1;
r1 = r1->next;
}
else
{
r3->next=r2;
r3= r2;
r2 = r2->next;
}
}
if(r1==NULL)
{
while(r2!=NULL){
r3->next = r2;
r3= r2;
r2=r2->next;}
}
else{
while(r1!=NULL){
r3->next =r1;
r3 = r1;
r1 = r1->next;}
}
}
void display(Linklist *&head)//链表的展示
{
Linklist *r;
r=head->next;
while(r!=NULL)
{
cout<<r->data<<" ";
r=r->next;
}
cout<<endl;
}
void sortlist(Linklist *&head)//选择排序
{
Linklist *p,*q,*small;
int temp;
for(p = head->next; p->next!=NULL;p=p->next)
{
small = p;
for(q=p->next;q;q=q->next)
{
if(q->data<small->data)
{
small = q;
}
}
if(small!=p)
{
temp = p->data;
p->data =small->data;
small->data = temp;
}
}
}
int main()
{
Linklist *head,*head1,*head2;
init(*&head1);
init(*&head);
init(*&head2);
int n1;int n2;
int a[30],b[30];
cin>>n1>>n2;
for(int i = 0;i<n1;i++)
cin>>a[i];
for(int i = 0;i<n2;i++)
cin>>b[i];
createlist(*&head1,n1,a);
createlist(*&head2,n2,b);
mergelist(*&head1,*&head2);
display(*&head1);
sortlist(*&head1);
display(*&head1);
return 0;
}