合并链表时出错
#include "stdio.h"#include "malloc.h"
typedef struct node{
int data;
struct node *next;
}NODE;
/*typedef struct node2{
int data2;
struct node *next2;
}NODE2*/;
NODE *create_list();
print_list(NODE *head);
NODE *connect_list(NODE *,NODE *);
main()
{
NODE *a1,*a2,*c;
a1=create_list();
a2=create_list();
printf("链表一为:");
print_list(a1);
printf("链表二为:");
print_list(a2);
c=connect_list(a1,a2);
printf("合并后的链表为");
print_list(c);
}
NODE *create_list()
{
int a,b;
NODE *head,*p,*q;
head=(NODE *)malloc(sizeof(NODE)); //分别申请两个头结点
q=head;
do{
printf("请输入链表的节点数:");
scanf("%d",&a);
}while(a<0);
printf("请输入链表的各节点的值:");
while(a>0)
{
scanf("%d",&b);
p=(NODE *)malloc(sizeof(NODE));
p->data=b;
q->next=p;
q=p;
a--;
}
q->next=NULL;
return head;
}
print_list(NODE *head) //输出链表
{
NODE *p;
p=head->next;
while(p!=NULL)
{
printf("%-5d",p->data);
p=p->next;
}
printf("\n");
}
NODE *connect_list(NODE *head1,NODE *head2) //连接链表
{
NODE *p1,*p2;
p1=head1->next;
p2=head2->next;
while(p1!=NULL)
{
p1=p1->next;
}
/* while(p2!=NULL){
p2=p2->next;
}*/
p1->next=p2;
//p2->next=NULL;
free(head2);
return head1;
}