#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct node
{
int data;
struct node *next;
}Node;
void Creatlist(Node head, int n)
{
Node p;
p = (Node)malloc(sizeof(Node));
p = head;
for(int i=0; i<n; i++){
p->next = (Node)malloc(sizeof(Node));
scanf("%d", &p->next->data);
p = p->next;
}
p->next = NULL;
}
Node *hebinglist(Node *head1,Node *head2)
{
if(!head1)
return head2;
if(!head2)
return head1;
if(head1->data <= head2->data){
head1->next = hebinglist(head1->next,head2);
return head1;
}
if(head1->data > head2->data){
head2->next = hebinglist(head1,head2->next);
return head2;
}
};
void Outputlist(Node *head)
{
Node *p = head->next;
while(p){
printf("%d ", p->data);
p = p->next;
}
}
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct node
{
int data;
struct node* next;
}Node;
void Creatlist(Node** head, int n) //修改
{
Node *p;
(*head) = (Node*)malloc(sizeof(Node));//修改
p = (*head); //修改
for (int i = 0; i < n; i++) {
p->next = (Node*)malloc(sizeof(Node));
scanf("%d", &p->next->data);
p = p->next;
}
p->next = NULL;
}
Node* hebinglist(Node* head1, Node* head2)
{
if (!head1)
return head2;
if (!head2)
return head1;
if (head1->data <= head2->data) {
head1->next = hebinglist(head1->next, head2);
return head1;
}
else { //if (head1->data > head2->data) { //修改
head2->next = hebinglist(head1, head2->next);
return head2;
}
} //;
void Outputlist(Node* head)
{
Node* p = head->next;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
int m, n;
Node* h1, * h2, * h3;
scanf("%d%d", &m, &n);
Creatlist(&h1, m);
//Outputlist(h1);
Creatlist(&h2, n);
//Outputlist(h2);
h3 = (Node*)malloc(sizeof(Node));
h3->next = hebinglist(h1->next, h2->next);
Outputlist(h3);
return 0;
}