指针完全不会用了,为了面试,写写~~
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct node{
int data;
node * next;
};
node *creat(){
struct node *head, *tail, *p;
head = NULL;
tail = NULL;
p = NULL;
int aa, bb;
while( scanf("%d", &aa) && aa!= 0){ // 输入为0时停止
p = (struct node *)malloc(sizeof(struct node));
p->data = aa;
if( head == NULL){
head = p;
tail = head;
}
else {
tail->next = p;
tail = p;
}
}
tail->next = NULL;
return head;
}
node *merge_sort(node *l1, node *l2){
node *tmp1, *tmp2, *tmp, *head, *tail;
if( l1->data <= l2->data){
head = l1;
tmp1 = l1->next;
tmp2 = l2;
}
else{
head = l2;
tmp1 = l1;
tmp2 = l2->next;
}
tail = head;
while(1){
if( tmp1 == NULL){
tail = tail->next = tmp2;
break;
}
if( tmp2 == NULL){
tail = tail->next = tmp1;
break;
}
if( (tmp1->data) <= (tmp2->data)){
tail = tail->next = tmp1;
tmp1 = tmp1->next;
}
else{
tail = tail->next = tmp2;
tmp2 = tmp2->next;
}
}
return head;
}
void print(node * tmp){
while( tmp != NULL){
cout<<tmp->data<<" ";
tmp = tmp->next;
}
printf("\n");
}
void sort(node *p){
node *head, *tail, *tmp1, *tmp2;
int aa;
for( tmp1 = p; tmp1!= NULL; tmp1 = tmp1->next){
for( tmp2 = tmp1->next; tmp2!= NULL; tmp2= tmp2->next){
if( tmp1->data >= tmp2->data){
aa = tmp1->data;
tmp1->data = tmp2->data;
tmp2->data = aa;
}
}
}
}
int main(){
freopen("1.txt", "r", stdin);
node *l1, *l2 , *tmp;
l1 = creat();
l2 = creat();
sort(l1);
sort(l2);
tmp = merge_sort(l1, l2);
print(tmp);
//l2 = creat();
return 0;
}