Doubly linked list

Nodes in a doubly linked list are linked in both directions using next and previous pointer. Two consecutive elements are linked by previous and next pointer. Thus it is possible to traverse any direction from head to tail or from tail to head. The limitation of single linked list thus has been eliminated and most of the practical and applied list has this type of structure inside it. Find the visual representation of the doubly linked list in the below figure.

Doubly linked list - pictorial view

Doubly Linked list

Doubly linked list source code

#include <stdio.h>
#include <conio.h>
typedef struct _dlist_node
{
  int node_value;
  struct _dlist_node * prev;
  struct _dlist_node * next;
10  }dlist_node;
11 
12  int main(int argc, char *argv[])
13  {
14    int key, i;
15    dlist_node *head, *temp, *current, *tail;
16    head = NULL;
17    temp = NULL;
18    current = NULL;
19    printf ("Doubly linked list demo application\n");
20    do
21    {
22      printf("Add a node [y/n] : ");
23      key = getch();
24      if(key == 'y')
25      {
26        temp = (dlist_node *)malloc(sizeof(dlist_node));
27        if(temp != NULL)
28        {
29          printf ("Value of this node : ");
30          scanf ("%d", &temp->node_value);
31          temp->prev = NULL;
32          temp->next = NULL;
33          if(head == NULL)
34          {
35            current = temp;
36            head = temp;
37          }
38          else
39          {
40            current->next = temp;
41            temp->prev = current;
42            current = temp;
43          }
44        }
45        else
46        {
47          printf ("Memory allocation error!");
48          return -1;
49        }
50 
51      }
52      else
53      {
54        break;
55      }
56 
57    } while (1);
58    tail = current;
59    current = head;
60    i = 0;
61    printf ("List Contains(sequence head to tail):\n");
62    while (current)
63    {
64      printf ("Node %d, Value: %d\n", i + 1, current->node_value);
65      i++;
66      current = current->next;
67    }
68 
69    current = tail;
70    i = 0;
71    printf ("List Contains(sequence tail to head):\n");
72    while(current)
73    {
74      printf ("Node %d, Value: %d\n", i + 1, current->node_value);
75      i++;
76      current = current->prev;
77    }
78 
79    return 0;
80  }

Output

Doubly 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

C and Data Structures, Data Structures using array, Linked List, Single Linked list, Doubly Linked list, Circular Linked list, Doubly Circular Linked list, Queue, reverse single linked list,

# 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