×

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

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 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.

What is the Use of Segment Trees in Data Structure?

Segment trees Segment trees are also called statistical trees in computer science. They are a type of tree data structure. Segment trees are used to store information regarding segments and intervals....

6 minutes read.

What Is Graph Data Structure

A graph is generally a set of vertices and edges or border that is mainly used to join these vertices. A graph is basically pictured as a cyclic tree in...

7 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Delete nodes from the linked list which have a greater value on the right side

Delete nodes from the linked list which have a greater value on the right side In this problem, we have given a singly linked list, and we need to remove all...

3 minutes read.

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

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.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Insertion Sort vs Selection Sort

In this article, we will discuss insertion sort, Selection sort and the basic differences between these two sorting techniques in detail: What is Insertion Sort? Insertion Sort – The insertion sort is...

5 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

Bookshop management system using file handling in C++

We see different software in every hospitals or library to manage their database. It is very important to store organization’s data. So we use this software. Now we are going...

5 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Binary Search Tree vs AVL Tree: Data Structure

Difference Between Binary Search Tree and AVL Tree Binary Search Tree: The binary search tree is a kind of binary tree data structure and it follows the conditions of binary...

3 minutes read.

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.