循环链表的链接
#include"stdio.h"#include"malloc.h"
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE create_cir();
NODE *connect(NODE *head1,NODE *head2);
main()
{
NODE *a,*b,*c,*d;
a=create_cir();
b=create_cir();
c=connect(a,b);
d=c;
printf("输入链接后的链表:");
while(d->next!=c)
{
d=d->next;
printf("%3d",d->data);
}
}
NODE create_cir()
{
NODE *head,*p,*q;
int a,n;
head=(NODE *)malloc(sizeof(NODE));
q=head;
printf("输入链表的结点数:");
scanf("%d",&n);
q->data=n;
if(n>0)
{
printf("输入链表:");
while(n>0)
{
scanf("%d",&a);
p=(NODE *)malloc(sizeof(NODE));
p->data=a;
q->next=p;
q=p;
n--;
}
}
q->next=head;
return(head);
}
NODE *connect(NODE *head1,NODE *head2)
{
NODE *p,*q;
p=head1->next;
while(p->next!=head1)
p=p->next;
q=head2->next;
while(q->next!=head2)
q=q->next;
p->next=head2->next;
q->next=head1;
free(head2);
return (head1);
}