Doubly Circular linked list

Doubly Circular linked list has both the properties of doubly linked list and circular linked list. Two consecutive elements are linked by previous and next pointer and the last node points to first node by next pointer and also the previous pointer of the head node points to the tail node. This two way pointer linking has eliminated all the short comings of all previous linked lists which have been discussed in the previous sections. Node traversal from any direction is possible and also jumping from head to tail or from tail to head is only one operation: head pointer previous is tail and also tail pointer next is head. Find the visual representation of the doubly circular linked list in the below figure.

Doubly Circular linked list - pictorial view

Doubly Circular Linked list

Doubly Circular linked list - Data Structure

typedef struct _dslist_node
{
  int node_value;              /* Data value */
  struct _dslist_node * prev;  /* pointer to previous node */
  struct _dslist_node * next;  /* pointer to next node */
}dslist_node;

Advantages

  1. List can be traversed bothways from head to tail as well as tail to head
  2. Being a circular linked list tail can be reached with one operation from head node

Disadvantages

  1. It takes slightly extra memory in each node to accomodate previous pointer

Practical Applications

  1. Managing songs playlist in media player applications
  2. Managing shopping cart in online shopping

Doubly Circular linked list source code

This is a minimal demo application to show the functionalities of doubly circular linked list. We are taking user's input to pupulate the nodes in the list. List is getting populated with the logic add at tail like a queue. We quit the node addition iteration as user press (n) and jumps to the display nodes section. This linked list can be iterated from head to tail as well as from tail to head. We are displaying list with these two iteration logic and finally freeing the list by deleting each node one by one. Deletion process can be from head to tail or vice versa and we are showing head to tail login here.

#include <stdio.h>
#include <conio.h>
/* Doubly Circular linked list node */
typedef struct _dslist_node
{
  int node_value;              /* Data value */
  struct _dslist_node * prev;  /* pointer to previous node */
  struct _dslist_node * next;  /* pointer to next node */
10 
11  }dslist_node;
12 
13  int main(int argc, char *argv[])
14  {
15    int key, i;
16    dslist_node *head, *temp, *current, *tail;
17    /* Initial condition for doubly circular linked list */
18    head = NULL;
19    tail = NULL;
20    temp = NULL;
21    current = NULL;
22    printf ("Doubly circular linked list demo application\n");
23    do
24    {
25      /* Populating doubly circular linked list till user escapes */
26      printf ("Add a node [y/n] : ");
27      key = getch();
28      if(key == 'y')
29      {
30        temp = (dslist_node *)malloc(sizeof(dslist_node));
31        if(temp != NULL)
32        {
33          printf ("Value of this node : ");
34          scanf ("%d", &temp->node_value);
35          /* Head null means list is empty */
36          if(head == NULL)
37          {
38            /* First node so, head = tail */
39            current = temp;
40            head = temp;
41            head->next = head;
42            head->prev = head;
43          }
44          else /* means list has head */
45          {
46            /* temp is the new tail */
47            current->next = temp;
48            temp->prev = current;
49            temp->next = head;
50            head->prev = temp;
51            current = temp;
52          }
53        }
54        else
55        {
56          printf ("Memory allocation error!");
57          return -1;
58        }
59      }
60      else /* User has given (n) exit population */
61      {
62        break;
63      }
64    } while (1);
65 
66    /* head to tail iteration */
67    tail = current;
68    current = head;
69    i = 0;
70    printf ("List Contains(sequence head to tail):\n");
71    do
72    {
73      printf ("Node %d, Value: %d\n", i + 1, current->node_value);
74      i++;
75      current = current->next;
76    } while (current != head);
77 
78    /* tail to head iteration */
79    current = tail;
80    i = 0;
81    printf ("List Contains(sequence tail to head):\n");
82    do
83    {
84      printf ("Node %d, Value: %d\n", i + 1, current->node_value);
85      i++;
86      current = current->prev;
87    }while (current != tail);
88 
89    /* Free a doubly linked list */
90    current = head;
91    while(current)
92    {
93      temp = current;
94      current = current->next;
95      free (temp);
96      if(current == tail)
97      {
98        break;
99      }
100    }
101    return 0;
102  }

Program Output

Doubly circular linked list demo application
Add a node [y/n] : y
Value of this node : 1
Add a node [y/n] : y
Value of this node : 10
Add a node [y/n] : y
Value of this node : 100
Add a node [y/n] : n
List Contains(sequence head to tail):
Node 1, Value: 1
Node 2, Value: 10
Node 3, Value: 100
List Contains(sequence tail to head):
Node 1, Value: 100
Node 2, Value: 10
Node 3, Value: 1

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

Linked List, Single Linked list, Doubly Linked list, Circular Linked list, Doubly Circular Linked list, Queue, reverse single linked list, delete single linked list, Stack,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan