知 L1、L2 分别为两循环单链表的头结点指针。要求设计一算法,用最快速度将两表合并成一个带头结点的循环单链表。

本文介绍了如何使用C语言实现链表的合并操作,通过Create()函数构建链表,Bing()函数实现两个链表的归并,展示了链表操作的基本步骤和数据结构的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值