#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
struct node {
int v;
struct node*pnext;
};
void input(struct node*phead);
void merge(struct node*phead1, struct node*phead2, struct node*phead3);
void output(struct node*phead);
int main() {
struct node*phead1 = (struct node*)malloc(sizeof(struct node));
phead1->pnext = NULL;
struct node*phead2 = (struct node*)malloc(sizeof(struct node));
phead2->pnext = NULL;
struct node*phead3 = (struct node*)malloc(sizeof(struct node));
phead3->pnext = NULL;
input(phead1);
input(phead2);
merge(phead1,phead2,phead3);
output(phead3);
return 0;
}
void input(struct node*phead) {
struct node*last = phead;
int n;
cout << "节点数:" << endl;
cin >> n;
cout << "输入节点:" << endl;
for (int i = 0; i < n; ++i) {
struct node*p = (struct node*)malloc(sizeof(node));
cin >> p->v;
p->pnext = NULL;
last->pnext = p;
last = p;
}
}
void merge(struct node*phead1, struct node*phead2, struct node*phead3) {
struct node*p1 = phead1->pnext, *p2 = phead2->pnext, *p3 = phead3;
while (p1&&p2) {
struct node*p = (struct node*)malloc(sizeof(struct node));
if (p1->v < p2->v) {
p->v = p1->v;
p->pnext = NULL;
p1 = p1->pnext;
}
else {
p->v = p2->v;
p->pnext = NULL;
p2 = p2->pnext;
}
p3->pnext = p;
p3 = p;
}
while (p1) {
struct node*p = (struct node*)malloc(sizeof(struct node));
p->v = p1->v;
p->pnext = NULL;
p1 = p1->pnext;
p3->pnext = p;
p3 = p;
}
while (p2) {
struct node*p = (struct node*)malloc(sizeof(struct node));
p->v = p2->v;
p->pnext = NULL;
p2 = p2->pnext;
p3->pnext = p;
p3 = p;
}
}
void output(struct node*phead) {
struct node*p = phead->pnext;
while (p) {
cout << " " << p->v;
p = p->pnext;
}
}
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
struct node {
int v;
struct node* pnext;
};
void input(struct node*head);
void merge(struct node*head1, struct node*head2);
void output(struct node*head);
int main() {
struct node*phead1 = (struct node*)malloc(sizeof(struct node));
phead1->pnext = NULL;
struct node*phead2 = (struct node*)malloc(sizeof(struct node));
phead2->pnext = NULL;
input(phead1);
input(phead2);
merge(phead1, phead2);
output(phead1);
return 0;
}
void input(struct node*phead) {
int n;
struct node*last = phead;
cout << "输入节点数:" << endl;
cin >> n;
cout << "输入节点:" << endl;
for (int i = 0; i < n; ++i) {
struct node*p= (struct node*)malloc(sizeof(struct node));
cin >> p->v;
p->pnext = NULL;
last->pnext = p;
last = p;
}
}
void merge(struct node*phead1, struct node*phead2) {
struct node*p1 = phead1, *p2 =phead2->pnext;
while (p1->pnext&&p2) {
if (p1->pnext->v > p2->v) {
struct node* tmp = p2->pnext;
p2->pnext=p1->pnext;
p1->pnext=p2;
p1 = p2;
p2 = tmp;
}
else p1 = p1->pnext;
}
if (p2) p1->pnext=p2;
}
void output(struct node*phead) {
cout << "合并结果:";
struct node*p = phead;
while (p->pnext) {
cout << " " << p->pnext->v;
p = p->pnext;
}cout <<endl;
}