代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
int num;
struct node *next;
};
struct node *creat(int n)
{
struct node *head, *p, *q;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
q = head;
for (int i = 0; i < n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->num);
q->next = p;
p->next = NULL;
q = p;
}
return head->next;
};
struct node *merge(struct node *head_1, struct node *head_2)
{
if(!head_1)
return head_2;
if(!head_2)
return head_1;
if(head_1->num <= head_2->num) {
head_1->next = merge (head_1->next, head_2);
return head_1;
}
if(head_1->num > head_2->num) {
head_2->next = merge (head_1, head_2->next);
return head_2;
}};
void list(struct node *head)
{
struct node *tail;
tail = head;
while(tail != NULL)
{
printf("%d\n",tail->num);
tail = tail->next;
}}
int main()
{
int n, m;
scanf("%d",&n);
struct node *head_1;
head_1 = creat(n);
scanf("%d",&m);
struct node *head_2;
head_2 = creat(m);
struct node *head_3;
head_3 = merge(head_1, head_2);
list(head_3);
return 0;}