Linked List Part 7 | Circular And Doubly Circular Linked List

 In this post we will deal with Circular Linked List and Doubly Circular Linked List, implement them in C language. 

Circular linked list : 
It can be called a modified version of simple linked list where the last node l (which pointed to NULL in simple linked list) points back to head creating a cycle. 

As we are traversing through this circular linked list, when we reach the last node and try to move onto next node of the last then we come back to the first node, as simple as that.
Linked List Part 7 | Circular And Doubly Circular Linked List
This circular property of it, gives the name Circular Linked List. This is clear from this image too.

Doubly Circular Linked List:
When we give a circular property to a Doubly Linked List, It becomes a Doubly Circular Linked List. It can also be called a hybrid of doubly linked list and circular linked list.

Here the next of last node is head and the previous of head is the last node.
Linked List Part 7 | Circular And Doubly Circular Linked List

This image will give you good clarity of doubly circular linked list.

If you have already read our previous posts on linked list then it would be very clear to you about how are linked lists made in C, so here I will only create a structure for circular linked list(CLL) and doubly circular linked list (DCLL). And then show how to link the nodes in main function. 

Code for circular and doubly circular linked list :
In this code we have created two different structures and showing the method to link the nodes in circular and doubly circular linked list.

Read the comments in code to understand it better...

#include <stdio.h>

//Compiler version gcc  6.3.0

// structute for circular linked list
struct cllNode{
  int data ;
  struct cllNode *nextAdd;
};

// structute for doubly circular linked list
struct dcllNode{
  int data ;
  struct dcllNode *nextAdd;
  struct dcllNode *prevAdd; 
  // pevious address to use 
  // property of doubly linked list
};

int main()
{
  // c_name refers to node of circular linked list
  // you can give another name ofvyour choice
  struct cllNode* c_head = (struct cllNode *)malloc(sizeof(struct cllNode));
  struct cllNode* c_one = (struct cllNode *)malloc(sizeof(struct cllNode));
  struct cllNode* c_two = (struct cllNode *)malloc(sizeof(struct cllNode));
  struct cllNode* c_last = (struct cllNode *)malloc(sizeof(struct cllNode));
  
  // setting data 
  c_head->data = 1;
  c_one->data = 2;
  c_two->data = 3;
  c_last->data = 4;
  
  // linking CLL nodes :
  c_head->nextAdd = c_one;
  c_one->nextAdd = c_two;
  c_two->nextAdd = c_last;
  
  // the most impprtant part of CLL
  // linking last node to head
  c_last->nextAdd = c_head;
  
  // creating nodes of doubly corcular linked list
  // dc_name;  dc => doubly circular
  struct dcllNode* dc_head = (struct dcllNode *)malloc(sizeof(struct dcllNode));
  struct dcllNode* dc_one = (struct dcllNode *)malloc(sizeof(struct dcllNode));
  struct dcllNode* dc_two = (struct dcllNode *)malloc(sizeof(struct dcllNode));
  struct dcllNode* dc_last = (struct dcllNode *)malloc(sizeof(struct dcllNode));
  
  // data for DCLL
  dc_head->data = 10;
  dc_one->data = 20;
  dc_two->data = 30;
  dc_last->data = 40;
  
  dc_head->nextAdd = dc_one;
  dc_head->prevAdd = dc_last; 
  // NOTE :
  // previous of head as last 
  
  dc_one->nextAdd = dc_two;
  dc_one->prevAdd = dc_head;
  dc_two->nextAdd = dc_last;
  dc_two->prevAdd = dc_one;
  
  // NOTE :
  // next of last as head
  dc_last->nextAdd = dc_head;
  dc_last->prevAdd = dc_two;
  
  return 0;
}

As these linked lists are made from previous versions only, so insertion and deletion is very much similar and you should try to inplement insertion and deletion on your own. You can refer the previous posts for insertion and deltion of nodes at various positions in linked list.

We will meet you in the next post with another topic on data structures and algorithms till then

#KeepLearningKeepGrowing

Post a Comment

0 Comments