求问,这个代码要怎么写
#include <stdio.h>
#include <stdlib.h>
#define MAX1 10
#define MAX2 6
typedef struct list
{
int num;
struct list *next;
}*link;
int data1[MAX1] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int data2[MAX2] = { 11, 12, 13, 14, 15, 16 };
link create_list(link head, int *data, int MAX)
{
link newnode;
link pointer;
int i;
head = malloc(sizeof(*head));
if(head==NULL)
printf("Memory allocate Failure!\n");
else
{
head->num = data[0];
head->next = NULL;
pointer = head;
for(i=1; i<MAX; ++i)
{
newnode = malloc(sizeof(*newnode));
if(newnode==NULL) break;
newnode->num = data[i];
newnode->next = NULL;
pointer->next = newnode;
pointer = newnode;
}
}
return head;
}
void free_list(link head)
{
link pointer;
while(head!=NULL)
{
pointer = head;
head = head->next;
free(pointer);
}
}
void print_list(link head)
{
if(head==NULL)
printf("empty!");
while(head!=NULL)
{
printf("[%d]", head->num);
head = head->next;
}
putchar('\n');
}
link concat(link head1, link head2)
{
link pointer = head1;
if(head1!=NULL && head2!=NULL)
{
while(pointer->next!=NULL)
pointer = pointer->next;
pointer->next = head2;
}
else if(head1==NULL && head2!=NULL)
head1 = head2;
return head1;
}
int main(void)
{
link head = NULL;
link head1 = NULL;
link head2 = NULL;
head1 = create_list(head1, data1, MAX1);
head2 = create_list(head2, data2, MAX2);
if(head1!=NULL && head2!=NULL)
{
printf("Input data :\n");
print_list(head1);
print_list(head2);
head = concat(head1, head2);
//head = concat(NULL, head2);
//head = concat(head1, NULL);
//head = concat(NULL, NULL); free(head1); free(head2);
printf("After concat:\n");
print_list(head);
free(head1);
}
return 0;
}