You are currently viewing Easy code for Circular Doubly Linked List

Easy code for Circular Doubly Linked List

Code for implementing Circular Doubly Linked List

This code is designed for implementing basic operations of Circular Doubly Linked List. In this code creation of Circular Doubly Linked list and two types of display techniques are implemented. There are three functions are written in this post, for linked list creation create() function and for displaying, fwddisp(), bckdisp() functions.

The code is written in very easy form. This will help you to understand the topic very easily.

For Such more codes click here and for video tutorial click here.

				
					// Circular Doubly Linked List Create and Display functions
#include <stdio.h>
#include<stdlib.h>
typedef struct Doubly
{
	int val;
	struct Doubly *next;
	struct Doubly *prev;
} db;

void create(db**,db**);
void fwddisp(db*);
void bckdisp(db*);
int main()
{
	db *head=NULL,*tail=NULL;
	create(&head,&tail);
	printf("\n\nForward Display : ");
	fwddisp(head);
	printf("\n\nBackward Display : ");
	bckdisp(tail);
	return 0;
}


void create(db **head,db **tail)
{
	db *temp;
	int g;
	char ch;
	while(1)
	{
		printf("\nEnter the value for the node : ");
		scanf("%d",&g);
		temp=(db*)malloc(sizeof(db));
		temp->val=g;
		temp->prev=temp->next=temp;
		if(*head==NULL)
			*head=*tail=temp;
		else
		{
			(*tail)->next=temp;
			temp->prev=*tail;
			temp->next=*head;
			*tail=temp;
			(*head)->prev=*tail;
		}
		printf("Do u want more node (y/n) : ");
		scanf(" %c",&ch);
		if(ch=='n' || ch=='N')
			break;
	}
}

void fwddisp(db *head)
{
	db *ptr=head;
	while(ptr->next!=head)
	{
		printf("%d ",ptr->val);
		ptr=ptr->next;
	}
	printf("%d",ptr->val);
}

void bckdisp(db *tail)
{
	db *ptr=tail;
	while(ptr->prev!=tail)
	{
		printf("%d ",ptr->val);
		ptr=ptr->prev;
	}
	printf("%d",ptr->val);
}
				
			

Leave a Reply