Circular Doubly Linked List
Circular Doubly Linked List
Circular Doubly Linked List
head
Representation of circular
doubly linked list
#include<stdio.h>
struct node
{
int data;
struct node *prev, *next;
};
struct node *n1, *n2;
n1 = (struct node *)malloc(sizeof(struct node));
n2 = (struct node *)malloc(sizeof(struct node));
n1data = 01;
n2data = 02;
n1next = n2;
n2prev = n1;
n2 next = n1;
Operations on circular DLL
• Insertion at beginning, at
1 end and specific location
• Deletion at beginning, at
2 end
void insert_end( )
{
struct node *new;
new = (struct node *)malloc(sizeof(struct node));
newdata = 02;
newnext = head;
headprev = new;
newprev = tail;
tailnext = new;
tail = new;
}
DELETION AT BEGINNING
void delete_begin( )
{
struct node *temp;
temp = head;
head = tempnext;
headprev = tail;
tailnext = head;
free(temp);
}
DELETION AT END
void delete_end( )
{
struct node *cur, *temp;
cur = tailnext;
if(tail == 0){
printf(“List is empty”);
}
else if(curnext == head)
free(cur);
else {
while(curnext != cur) {
temp = cur;
cur = curnext;
}
tempnext = head;
headprev = temp;
free(cur);
}
}
SEARCHING
void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr != NULL) {
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item) {
printf("item found at location %d",i+1);
flag=0;
}
else {
while (ptr->next != head)
{
if(ptr->data == item) {
printf("item found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0) {
printf("Item not found\n");
}
}
}
Travsersing
Void traverse ( )
{
struct node *temp;
temp = head;
if(temp == NULL)
{
printf(“List is empty”);
}
else {
while(tempnext != head)
{
printf(“%d “,tempdata);
temp = tempnext;
}
}
}
Applications of circular doubly linked list
– Used with data where we have to navigate front and
back.
– Circular doubly linked lists are used in
multiprocessing.
– Can be used in game development to implement a
loop of game objects.
– Used to implement an LRU (Least Recently Used)
cache.
– Can be used to implement a hash table.
Real-Time Applications:
• Music Player.
• Shopping-cart on online websites.
• Browser cache.
Advantages
• List can be traversed from both directions i.e. from head to
tail or from tail to head.
• Ease of data manipulation.
• Jumping from head to tail or vice versa takes O(1) time.
• Efficient insertion and deletion.
• Ability to handle circular buffers.
Dis-Advantages
• Requires additional memory.
• More complex than singly linked list.
• If not used properly, then the problem of infinite loop can
occur.
• Difficulty in detecting the end.
• Have a slight overhead of maintaining both the next and
previous pointers at each node.
• Not efficient for large datasets
THANK YOU!!