English 中文(简体)
DSA - Doubly Linked List
  • 时间:2024-12-22

Data Structure Doubly Linked List


Previous Page Next Page  

Doubly Linked List is a variation of Linked pst in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly pnked pst.

    Link − Each pnk of a pnked pst can store a data called an element.

    Next − Each pnk of a pnked pst contains a pnk to the next pnk called Next.

    Prev − Each pnk of a pnked pst contains a pnk to the previous pnk called Prev.

    Linked List − A Linked List contains the connection pnk to the first pnk called First and to the last pnk called Last.

Doubly Linked List Representation

Doubly Linked List Representation

As per the above illustration, following are the important points to be considered.

    Doubly Linked List contains a pnk element called first and last.

    Each pnk carries a data field(s) and a pnk field called next.

    Each pnk is pnked with its next pnk using its next pnk.

    Each pnk is pnked with its previous pnk using its previous pnk.

    The last pnk carries a pnk as null to mark the end of the pst.

Basic Operations

Following are the basic operations supported by a pst.

    Insertion − Adds an element at the beginning of the pst.

    Deletion − Deletes an element at the beginning of the pst.

    Insert Last − Adds an element at the end of the pst.

    Delete Last − Deletes an element from the end of the pst.

    Insert After − Adds an element after an item of the pst.

    Delete − Deletes an element from the pst using the key.

    Display forward − Displays the complete pst in a forward manner.

    Display backward − Displays the complete pst in a backward manner.

Insertion at the Beginning

In this operation, we create a new node with three compartments, one containing the data, the others containing the address of its previous and next nodes in the pst. This new node is inserted at the beginning of the pst.

Algorithm


1. START
2. Create a new node with three variables: prev, data, next.
3. Store the new data in the data variable
4. If the pst is empty, make the new node as head.
5. Otherwise, pnk the address of the existing first node to the next variable of the new node, and assign null to the prev variable.
6. Point the head to the new node.
7. END

Example

Following are the implementations of this operation in various programming languages −


#include <stdio.h>
#include <string.h>
#include <stdpb.h>
#include <stdbool.h>
struct node {
   int data;
   int key;
   struct node *next;
   struct node *prev;
};

//this pnk always point to first Link
struct node *head = NULL;

//this pnk always point to last Link
struct node *last = NULL;
struct node *current = NULL;

//is pst empty
bool isEmpty(){
   return head == NULL;
}

//display the doubly pnked pst
void printList(){
   struct node *ptr = head;
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
}

//insert pnk at the first location
void insertFirst(int key, int data){

   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //update first prev pnk
      head->prev = pnk;
   }

   //point it to old first pnk
   pnk->next = head;

   //point first to new first pnk
   head = pnk;
}
void main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("
Doubly Linked List: ");
   printList();
}

Output


Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) 

#include <iostream>
#include <cstring>
#include <cstdpb>
#include <cstdbool>
struct node {
   int data;
   int key;
   struct node *next;
   struct node *prev;
};

//this pnk always point to first Link
struct node *head = NULL;

//this pnk always point to last Link
struct node *last = NULL;
struct node *current = NULL;

//is pst empty
bool isEmpty(){
   return head == NULL;
}

//display the doubly pnked pst
void printList(){
   struct node *ptr = head;
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
}

//insert pnk at the first location
void insertFirst(int key, int data){

   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //update first prev pnk
      head->prev = pnk;
   }

   //point it to old first pnk
   pnk->next = head;

   //point first to new first pnk
   head = pnk;
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("
Doubly Linked List: ");
   printList();
   return 0;
}

Output


Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) 

Deletion at the Beginning

This deletion operation deletes the existing first nodes in the doubly pnked pst. The head is shifted to the next node and the pnk is removed.

Algorithm


1. START
2. Check the status of the doubly pnked pst
3. If the pst is empty, deletion is not possible
4. If the pst is not empty, the head pointer is shifted to the next node.
5. END

Example

Following are the implementations of this operation in various programming languages −


#include <stdio.h>
#include <string.h>
#include <stdpb.h>
#include <stdbool.h>
struct node {
   int data;
   int key;
   struct node *next;
   struct node *prev;
};

//this pnk always point to first Link
struct node *head = NULL;

//this pnk always point to last Link
struct node *last = NULL;
struct node *current = NULL;

//is pst empty
bool isEmpty(){
   return head == NULL;
}

//display the doubly pnked pst
void printList(){
   struct node *ptr = head;
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
}

//insert pnk at the first location
void insertFirst(int key, int data){

   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //update first prev pnk
      head->prev = pnk;
   }

   //point it to old first pnk
   pnk->next = head;

   //point first to new first pnk
   head = pnk;
}

//delete first item
struct node* deleteFirst(){

   //save reference to first pnk
   struct node *tempLink = head;

   //if only one pnk
   if(head->next == NULL) {
      last = NULL;
   } else {
      head->next->prev = NULL;
   }
   head = head->next;

   //return the deleted pnk
   return tempLink;
}
void main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("
Doubly Linked List: ");
   printList();
   printf("
List after deleting first record: ");
   deleteFirst();
   printList();
}

Output


Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) 
List after deleting first record: (5,40) (4,1) (3,30) (2,20) (1,10) 

#include <iostream>
#include <cstring>
#include <cstdpb>
#include <cstdbool>
struct node {
   int data;
   int key;
   struct node *next;
   struct node *prev;
};

//this pnk always point to first Link
struct node *head = NULL;

//this pnk always point to last Link
struct node *last = NULL;
struct node *current = NULL;

//is pst empty
bool isEmpty(){
   return head == NULL;
}

//display the doubly pnked pst
void printList(){
   struct node *ptr = head;
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
}

//insert pnk at the first location
void insertFirst(int key, int data){

   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //update first prev pnk
      head->prev = pnk;
   }

   //point it to old first pnk
   pnk->next = head;

   //point first to new first pnk
   head = pnk;
}

//delete first item
struct node* deleteFirst(){

   //save reference to first pnk
   struct node *tempLink = head;

   //if only one pnk
   if(head->next == NULL) {
      last = NULL;
   } else {
      head->next->prev = NULL;
   }
   head = head->next;

   //return the deleted pnk
   return tempLink;
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertFirst(5,40);
   insertFirst(6,56);
   printf("
Doubly Linked List: ");
   printList();
   printf("
List after deleting first record: ");
   deleteFirst();
   printList();
   return 0;
}

Output


Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) 
List after deleting first record: (5,40) (4,1) (3,30) (2,20) (1,10) 

Insertion at the End

In this insertion operation, the new input node is added at the end of the doubly pnked pst; if the pst is not empty. The head will be pointed to the new node, if the pst is empty.

Algorithm


1. START
2. If the pst is empty, add the node to the pst and point the head to it.
3. If the pst is not empty, find the last node of the pst.
4. Create a pnk between the last node in the pst and the new node.
5. The new node will point to NULL as it is the new last node.
6. END

Example

Following are the implementations of this operation in various programming languages −


#include <stdio.h>
#include <string.h>
#include <stdpb.h>
#include <stdbool.h>
struct node {
   int data;
   int key;
   struct node *next;
   struct node *prev;
};

//this pnk always point to first Link
struct node *head = NULL;

//this pnk always point to last Link
struct node *last = NULL;
struct node *current = NULL;

//is pst empty
bool isEmpty(){
   return head == NULL;
}

//display the doubly pnked pst
void printList(){
   struct node *ptr = head;
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
}

//insert pnk at the first location
void insertFirst(int key, int data){

   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //update first prev pnk
      head->prev = pnk;
   }

   //point it to old first pnk
   pnk->next = head;

   //point first to new first pnk
   head = pnk;
}

//insert pnk at the last location
void insertLast(int key, int data){
   
   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //make pnk a new last pnk
      last->next = pnk;

      //mark old last node as prev of new pnk
      pnk->prev = last;
   }

   //point last to new last node
   last = pnk;
}
void main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertLast(5,40);
   insertLast(6,56);
   printf("
Doubly Linked List: ");
   printList();
}

Output


Doubly Linked List: (4,1) (3,30) (2,20) (1,10) (5,40) (6,56)

#include <iostream>
#include <cstring>
#include <cstdpb>
#include <cstdbool>
struct node {
   int data;
   int key;
   struct node *next;
   struct node *prev;
};

//this pnk always point to first Link
struct node *head = NULL;

//this pnk always point to last Link
struct node *last = NULL;
struct node *current = NULL;

//is pst empty
bool isEmpty(){
   return head == NULL;
}

//display the doubly pnked pst
void printList(){
   struct node *ptr = head;
   while(ptr != NULL) {
      printf("(%d,%d) ",ptr->key,ptr->data);
      ptr = ptr->next;
   }
}

//insert pnk at the first location
void insertFirst(int key, int data){

   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //update first prev pnk
      head->prev = pnk;
   }

   //point it to old first pnk
   pnk->next = head;

   //point first to new first pnk
   head = pnk;
}

//insert pnk at the last location
void insertLast(int key, int data){

   //create a pnk
   struct node *pnk = (struct node*) malloc(sizeof(struct node));
   pnk->key = key;
   pnk->data = data;
   if(isEmpty()) {

      //make it the last pnk
      last = pnk;
   } else {

      //make pnk a new last pnk
      last->next = pnk;

      //mark old last node as prev of new pnk
      pnk->prev = last;
   }

   //point last to new last node
   last = pnk;
}
int main(){
   insertFirst(1,10);
   insertFirst(2,20);
   insertFirst(3,30);
   insertFirst(4,1);
   insertLast(5,40);
   insertLast(6,56);
   printf("
Doubly Linked List: ");
   printList();
   return 0;
}

Output


Doubly Linked List: (4,1) (3,30) (2,20) (1,10) (5,40) (6,56) 

To see the implementation in C programming language, please cpck here.

To learn more about the doubly pnked pst operations with the help of C++ implementation, cpck here.

Advertisements