×

Doubly Linked List

Doubly Linked List

Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either forward or backward as compared to the singly linked list. For traversing in both directions, as we said we have two pointers e.g. Next – the next pointer contains the address of next node; Prev – the prev pointer contains the address of the previous node.

Doubly Linked List

Operations on Doubly Linked List

  • Insert a node in the linked list:

We can insert a node into the linked list at the front, the end or anywhere in the linked list. The time complexity of these operations is as follows:

  • If we insert the node at the front of linked list; takes O(1)
  • If we insert the node at the end of linked list; takes O(n)
  • If we insert the node anywhere in the linked list; takes O(n)
  • Remove a node from the linked list:

We can remove a node from the front of the linked list, the end or anywhere of the linked list. The time complexity of these operations is as follows:

  • If we remove the node from the front of linked list; takes O(1)
  • If we remove the node from the end of linked list; takes O(n)
  • If we remove the node from anywhere in the linked list; takes O(n)
  • Search a node in the linked list:

 Searching a node in the linked list takes O(n) time in the worst case.

Doubly Linked List Program in C Language

 #include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 struct node
 {
 int info;
 struct node *next;
 struct node *back;
 };
 struct node *start = NULL;
 struct node *uni = NULL;
 void begin(int item)
 {
     struct node *p, *t, *k;
     if(start == NULL)
     {
     p = start;
     t = (struct node *)malloc(sizeof(struct node ));
     start = t;
     start -> info = item;
     start -> next = p;
     start -> back = NULL;
     }
     else
     {
     k = start -> back;
     p = start;
     t = (struct node *)malloc(sizeof(struct node ));
     start = t;
     start -> info = item;
     start -> next = p;
     start -> back = NULL;
     k = start;
     }
 }
 void Specificposition(int item,int pos)
 {
     int i;
 struct node *t, *p, *temp;
 p = start;
 t = (struct node *)malloc(sizeof(struct node ));
 for(i = 1; i<pos-1; i++)
 {
     p = p -> next;
 }
   temp = p -> next;
   t -> back = p;
   t -> next = temp;
   t -> info = item;
   p -> next = t;
 }
 void End(int item)
 {
 struct node *t,*p;
 t = (struct node *)malloc(sizeof(struct node ));
 if(start == NULL)
 {
 start = t;
 start -> info = item;
 start -> next = NULL;
 start -> back = NULL;
 return;
 }
 else
 {
 struct node *p = start,*k;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 k = p;
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 p -> back = k;
 uni = p;
 }
 }
 void delbegin()
 {
     struct node *t,*p;
     p = start;
     t = start -> next;
     start = t;
     start -> back = NULL;
     free(p);
 }
 void delend()
 {
      struct node *t;
      t = start;
      while(t -> next -> next !=  NULL)
      {
          t = t -> next;
      }
      t -> next = NULL;
      return;
 }
 void delspecificposition(int pos)
 {
     int i;
     struct node *t, *p;
     t = start;
    for(i = 1; i<pos-1; i++)
    {
        t = t -> next;
    }
    p = t -> next -> next;
    p -> back = t;
    t -> next = t -> next -> next;
     return;
 }
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     while(t -> next !=  NULL)
                                     {
                         printf("%d< -> ",t -> info);
                         t = t -> next;
                         }
                         printf("%d\n",t -> info);
 }
 void main()
 {
 int n,data,i,item,pos;
 while(1)
 {
 printf(" 1. Insert at begin");
 printf("\n 2. Insert at specific position");
 printf("\n 3. Insert at End");
 printf("\n 4. Delete at beginning");
 printf("\n 5. Delete specific position");
 printf("\n 6. Delete at End");
 printf("\n 7. Traverse the list");
 printf("\n 8. Exit\n");
 printf("Enter the choice:-\n");
 scanf("%d",&n);
 switch(n)
 {
 case 1:
        printf("Enter the item do wanna insert:-\n");
        scanf("%d",&item);
        begin(item);
        break;
 case 2:
      printf("Enter the position where do wanna insert:-\n");
      scanf("%d",&pos);
      printf("enter the item do wanna insert at position:-\n");
      scanf("%d",&item);
      Specificposition(item,pos);
      break;
 case 3:
        printf("Enter the item do wanna insert at end:-\n");
        scanf("%d",&item);
        End(item);
        break;
 case 4:
        delbegin();
        break;
 case 5:
          printf("Enter the position which do wanna delete :-\n");
          scanf("%d",&pos);
          delspecificposition(pos);
          break;
 case 6:
       delend();
       break;
 case 7:
        printf("your list is:");
        traverse(start);
        getch();
        break;
 case 8:
         exit(0);
 }
 }
 } 

Output

Doubly Linked List
Doubly Linked List

Related Topics

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Stack vs Heap Memory Allocation Data Structure

Difference Between Stack and Heap Memory Allocation Stack Memory Stack memory allocation is a way to use the system memory as a temporary storage of the data which is act like last-in-first-out...

3 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

8 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Splay Tree

Splay Tree A splay tree is a self-balanced or self-adjusted binary search tree. We can say, Splay Tree is used in some cases where some elements or data are accessed more...

8 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

How to Start Learning DSA

All programmer experiences a point along the way where they wish they could approach a problem in a more effective manner. They finally learn about the terminology DSA while trying...

10 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.