#include<stdio.h>
#include<stdlib.h>
struct LB
{
int data;
struct LB *next;
};
struct LB *Create();
void Shu(struct LB *head);
struct LB *Bing(struct LB *head1,struct LB *head2);
int main()
{
struct LB *L1,*L2,*L;
printf("请输入链表L1的表内元素(输入-1回车结束输入):\n");
L1=Create();
printf("请输入链表L2的表内元素(输入-1回车结束输入):\n");
L2=Create();
printf("链表创建结果如下:\n");
L=Bing(L1,L2);
Shu(L);
return 0;
}
struct LB *Create()
{
struct LB *head,*p,*p1;
head=(struct LB *)malloc(sizeof(struct LB));
head->next =NULL;
p=(struct LB *)malloc(sizeof(struct LB));
scanf("%d",&p->data );
p->next =NULL;
while(p->data !=-1)
{
if(head->next ==NULL)
{
head->next =p;
p1=p;
}
else
{
p1->next =p;
p1=p;
}
p=(struct LB *)malloc(sizeof(struct LB));
scanf("%d",&p->data );
p->next =NULL;
}
p1->next =head;
return head;
}
void Shu(struct LB *head)
{
struct LB *p;
p=head->next ;
while(p!=head)
{
printf("%-3d",p->data );
p=p->next ;
}
putchar('\n');
}
struct LB *Bing(struct LB *head1,struct LB *head2)
{
struct LB *p1,*p2;
p1=head1;
p2=head2;
while(p1->next !=head1)
p1=p1->next ;
while(p2->next !=head2)
p2=p2->next ;
p1->next =head2->next ;
p2->next =head1;
return head1;
}
知 L1、L2 分别为两循环单链表的头结点指针。要求设计一算法,用最快速度将两表合并成一个带头结点的循环单链表。
于 2022-10-19 14:58:33 首次发布