文章
29
粉丝
598
获赞
17
访问
284.8k
#include
#include
#include
#define len sizeof(struct num)
struct num
{
int x;
struct num *next;
};
int n,m;
struct num * input1(void)
{
struct num *head;
struct num *p1,*p2;
int i=1;
scanf("%d",&n);
p1=p2=(struct num *)malloc(len);
scanf("%d",&p1->x);
head=p1;
while(i {
p1=(struct num *)malloc(len);
scanf("%d",&p1->x);
p2->next=p1;
p2=p1;
i++;
}
p2->next=NULL;
return head;
}
struct num * input2(void)
{
struct num *head;
struct num *p1,*p2;
int i=1;
scanf("%d",&m);
p1=p2=(struct ...
登录后发布评论
#include <iostream>
using namespace std;
//http://www.noobdream.com/DreamJudge/Contest/1/1025/?Problem=T
//给定两个元素有序(从小到大)的链表,要求将两个链表合并成一个有序(从小到大)链表,
typedef struct node {
int num;
struct node* next;
}node ,* pnode;
int main(void) {
int n1, n2;
pnode p1, p2,result, r;
p1 = (pnode)malloc(sizeof(node));
p1->num = 0;
p1->next = NULL;
p2 = (pnode)malloc(sizeof(node));
p2->num = 0;
p2->next = NULL;
result = (pnode)malloc(sizeof(node));
r = NULL;
cin >> n1;
r = p1;
for (int i = 0; i < n1; i++) {
pnode s = (pnode)malloc(sizeof(node));
cin >> s->num;
s->next = NULL;
r->next = s;
r = s;
}
cin >> n2;
r = p2;
for (int i = 0; i < n2; i++) {
pnode s = (pnode)malloc(sizeof(node));
cin >> s->num;
s->next = NULL;
r->next = s;
r = s;
}
p1 = p1->next;
p2 = p2->next;
r = result;
while (p1) {
if (!p2) break;
while (p2) {
if (!p1) break;
if (p1->num < p2->num) {
pnode s = (pnode)malloc(sizeof(node));
s->num = p1->num;
s->next = NULL;
r->next = s;
r = s;
p1 = p1->next;
}
else {
pnode s = (pnode)malloc(sizeof(node));
s->num = p2->num;
s->next = NULL;
r->next = s;
r = s;
p2 = p2->next;
}
}
}
if (!p1) {
while (p2) {
pnode s = (pnode)malloc(sizeof(node));
s->num = p2->num;
s->next = NULL;
r->next = s;
r = s;
p2 = p2->next;
}
}else {
while(p1){
pnode s = (pnode)malloc(sizeof(node));
s->num = p1->num;
s->next = NULL;
r->next = s;
r = s;
p1 = p1->next;
}
}
result = result->next;
while (result) {
cout << result->num;
if (result->next != NULL) cout << " ";
result = result->next;
}
return 0;
}