Home » C programs

Implement Circular Doubly Linked List | C program

Here, we are going to learn how to implement circular doubly linked list using C program?
Submitted by Piyas Mukherjee, on January 30, 2019

Solution:

Input: The data of each node

Output: The circular doubly linked list

Data structure used: A circular doubly linked list where each node contains a data part, say data and two link parts say prev (to store the address of immediate previous node) and say next (to store the address of immediate next node).

Algorithm:

Begin
head=NULL
//get a memory block of type node and store it in the pointer temp
temp=getnode(node)		
If(temp!=NULL)
	Print("Enter the element in the list : ",temp->data)
	temp->next=head;
	If(head=NULL)
		head=temp
		temp->prev=head
		temp->next=head
	Else
		temp1=head;
		while(temp1->next!=head)
			begin
			temp1=temp1->next;
		End while
			temp1->next=temp;
			temp->prev=temp1;
	End IF-Else	
Else
	Printf ("Memory not avilable...node allocation is not possible")
End IF-ELSE
End

C code:

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

//cicular double linked list structure
typedef struct list 
{
	int data; 
	struct list *next;
	struct list *prev;
}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;
	
	//building circular double 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=head;
			if(head==NULL)
			{	
				head=temp;
				temp->prev=head;
				temp->next=head;
			}
			else
			{
				temp1=head;
				while(temp1->next!=head)
				{
					temp1=temp1->next;
				}
				temp1->next=temp;
				temp->prev=temp1;
			}
		}
		else
		{
		printf("\nMemory not avilable...node allocation is not possible");
		}
		printf("\nIf you wish to add more data on the list enter 1 : ");
		scanf("%d",&choice);
	}while(choice==1);
	
	display(head);
	
	return 0;
}

Output

Enter the element in the list : 1

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

Enter the element in the list : 2

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

Enter the element in the list : 3

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

Enter the element in the list : 4

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

Enter the element in the list : 5

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.