/*有两个循环单链表,链表头指针分别为h1和h2,编写一个函数将链表h2链接到链表h1之后,
要求连接后的链表仍保持循环链表形式*/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList createHeadList(LinkList &L)
{
ElemType x;
L = (LinkList)malloc(sizeof(LNode));
LNode *r = L;
scanf("%d",&x);
while(x!=9999)
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d",&x);
}
r->next = L;
return L;
}
LinkList connectList(LinkList &h1,LinkList &h2)
{
LNode *r1 = h1;
LNode *r2 = h2;
while(r1->next != h1)
r1 = r1->next;
while(r2->next != h2)
r2 = r2->next;
r1->next = h2;
r2->next = h1;
return h1;
}
bool printList(LinkList L)
{
LNode *p = L->next;
while(p!=L)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return true;
}
void main()
{
LinkList H1,H2;
createHeadList(H1);
createHeadList(H2);
printf("After:\n");
connectList(H1,H2);
printList(H1);
}
链接两个循环单链表
最新推荐文章于 2022-02-20 12:56:03 发布