小程序 给定链接截取参数_将给定的单链接列表转换为循环列表| C程序

该博客介绍了如何使用C语言将给定的单链表转换为循环链表。算法包括接收单链表的头指针作为输入,并通过修改最后一个节点的指针使其指向链表的开头,从而创建循环链表。伪代码和C代码实现也被提供。
摘要由CSDN通过智能技术生成

小程序 给定链接截取参数

Algorithm:

算法:

Function: slltocll()

函数:slltocll()

Input: A singly linked list whose address of the first node is stored in a pointer, say head

输入:一个单链表 ,其第一个节点的地址存储在指针中,例如head

Output: The same circular linked list

输出:相同的循环链表

Data structure used: Singly linked list where each node contains a data element say data, and the address of the immediate next node say next, with Head holding the address of the first node.

所使用的数据结构:单链列表,其中每个节点包含一个数据元素,例如data ,直接下一个节点的地址说next ,其中Head保留第一个节点的地址。

Pseudo code:

伪代码:

Begin
temp=head
while(temp->next!=NULL)
	begin
	temp=temp->next
	end While
//link head to the next of last node 
temp->next=head
End

C code:

C代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
	int data;
	struct list *next;
}node;

void display(node *temp)
{
	//now temp1 is head basically
	node *temp1=temp; 
	printf("The list is as follows :\n%d->",temp->data);
	temp=temp->next;
	//which not circle back to head node 
	while(temp!=temp1) 
	{
		printf("%d->",temp->data);
		temp=temp->next;
	}
	printf("%d\n",temp1->data);
	
	return;
}

int main(){
	node *head=NULL,*temp,*temp1;
	int choice,count=0,key;

	//Taking the linked list as input
	do
	{
		temp=(node *)malloc(sizeof(node));
		if(temp!=NULL)
		{
			printf("\nEnter the element in the list : ");
			scanf("%d",&temp->data);
			temp->next=NULL;
			if(head==NULL)
			{	
				head=temp;
			}
			else
			{
				temp1=head;
				while(temp1->next!=NULL)
				{
					temp1=temp1->next;
				}
				temp1->next=temp;
			}
		}
		else
		{
			printf("\nMemory not avilable...node allocation is not possible");
		}
		printf("\nIf you wish to add m ore data on the list enter 1 : ");
		scanf("%d",&choice);
	}while(choice==1);
	
	//In order to convert a singly linked list to a 
	//circular singly linked list we just need to copy 
	//the address of the head node to the next of the last node
	temp=head;
	//travarsing to the last node
	while(temp->next!=NULL)	
	{
		temp=temp->next;
	}
	//the address of the head is copied to the next part 
	//of the last node.....now it is a circular singly linked list
	temp->next=head;	
	display(head);
	
	return 0;
}

Output

输出量

Enter the element in the list : 1

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 2

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 3

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 4

If you wish to add m ore data on the list enter 1 : 1

Enter the element in the list : 5

If you wish to add m ore data on the list enter 1 : 0
The list is as follows :
1->2->3->4->5->1


翻译自: https://www.includehelp.com/c-programs/convert-a-given-singly-linked-list-to-a-circular-list.aspx

小程序 给定链接截取参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值