关于单向循环链表
刚开始看数据结构,自己写了几行,目的是输入两个未知长度大小的循环链表,然后把这两个 链表合并成一个新的循环链表。但是执行的时候发现,第一个链表 可以完整的输入进去,每次一开始输入第二个链表就死机,找了半天也没有发现问题在哪里。求各位大神帮忙看看那个输入链表的子函数是不是有问题。
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int a;
node * next;
};
node *input_linked_list(node *head,int *count)//输入链表
{
node *p,*last=NULL;
char c='y';int i=1;
while(1)
{
printf("please input the %d number:\t",i);
p=(node *)malloc(sizeof(node));
scanf("%d",&(p->a));getchar();
if(head==NULL)
{
head=p;
}
else if(i==1)
{
head->next=p;
}
if(last==NULL)last=p;
else
{
last->next=p;
last=p;
}
(*count)++;i++;
printf("continue? y&n\t");
scanf("%c",&c);
if(c=='n')
{
break;
}
}
last->next=head;
return last;
}
void print(node * link_head,node *link_last)
{
node *p=link_head;
while(p->next!=link_head)
{
printf("%d\t",p->next->a);
p=p->next;
}
printf("\n");
}
void combine(node *last1,node * last2)
{
node *temp;
temp=last1->next;
last1->next=last2->next;
last2->next=temp;
}
main()
{
node *head1,*head2,*last1,*last2;
int count1=0,count2=0;
printf("please input the first link.\n");
last1=input_linked_list(head1,&count1);
printf("the length of the first link is %d\n",count1);
printf("the element is :\n");
print(head1,last1);
printf("please input the second link.\n");
last2=input_linked_list(head2,&count2);
printf("the length of the second link is %d\n",count2);
combine(last1,last2);
print(last1->next,last2->next);
printf("\n");
}