Which of the following operations are easy efficient to do with a doubly linked list data structure?

Advantages, Disadvantages, and uses of Doubly Linked List

A Doubly Linked List[DLL] is a linear data structure that contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in a singly linked list. Below is the image to illustrate the same.

Advantages Of DLL:

  • Reversing the doubly linked list is very easy.
  • It can allocate or reallocate memory easily during its execution.
  • As with a singly linked list, it is the easiest data structure to implement.
  • The traversal of this doubly linked list is bidirectional which is not possible in a singly linked list.
  • Deletion of nodes is easy as compared to a Singly Linked List. A singly linked list deletion requires a pointer to the node and previous node to be deleted but in the doubly linked list, it only required the pointer which is to be deleted.

Disadvantages Of DLL:

  • It uses extra memory when compared to the array and singly linked list.
  • Since elements in memory are stored randomly, therefore the elements are accessed sequentially no direct access is allowed.

Uses Of DLL:

  • It is used in the navigation systems where front and back navigation is required.
  • It is used by the browser to implement backward and forward navigation of visited web pages that is a back and forward button.
  • It is also used to represent a classic game deck of cards.
  • It is also used by various applications to implement undo and redo functionality.
  • Doubly Linked List is also used in constructing MRU/LRU [Most/least recently used] cache.
  • Other data structures like stacks, Hash Tables, Binary trees can also be constructed or programmed using a doubly-linked list.
  • Also in many operating systems, the thread scheduler[the thing that chooses what process needs to run at which time] maintains a doubly-linked list of all processes running at that time.

Article Tags :
Data Structures
Linked List
Data Structures-Linked List
doubly linked list
Practice Tags :
Data Structures
Linked List
Read Full Article

Doubly Linked In C++

As in the singly linked list, the doubly linked list also has a head and a tail. The previous pointer of the head is set to NULL as this is the first node. The next pointer of the tail node is set to NULL as this is the last node.

A basic layout of the doubly linked list is shown in the below diagram.

In the above figure, we see that each node has two pointers, one pointing to the previous node and the other pointing to the next node. Only the first node [head] has its previous node set to null and the last node [tail] has its next pointer set to null.

As the doubly linked list contains two pointers i.e. previous and next, we can traverse it into the directions forward and backward. This is the main advantage of doubly linked list over the singly linked list.

Declaration

In C-style declaration, a node of the doubly linked list is represented as follows:

struct node { struct node *prev; int data; struct node *next; };

Apart from the above declaration, we can also represent a node in the doubly linked list as a class in C++. A doubly linked list is represented as a class when we use STL in C++. We can implement a doubly linked list using a class in Java as well.

Basic Operations

Following are some of the operations that we can perform on a doubly linked list.

Insertion

Insertion operation of the doubly linked list inserts a new node in the linked list. Depending on the position where the new node is to be inserted, we can have the following insert operations.

  • Insertion at front – Inserts a new node as the first node.
  • Insertion at the end – Inserts a new node at the end as the last node.
  • Insertion before a node – Given a node, inserts a new node before this node.
  • Insertion after a node – Given a node, inserts a new node after this node.

Deletion

Deletion operation deletes a node from a given position in the doubly linked list.

  • Deletion of the first node – Deletes the first node in the list
  • Deletion of the last node – Deletes the last node in the list.
  • Deletion of a node given the data – Given the data, the operation matches the data with the node data in the linked list and deletes that node.

Traversal

Traversal is a technique of visiting each node in the linked list. In a doubly linked list, we have two types of traversals as we have two pointers with different directions in the doubly linked list.

  • Forward traversal – Traversal is done using the next pointer which is in the forward direction.
  • Backward traversal – Traversal is done using the previous pointer which is the backward direction.

Reverse

This operation reverses the nodes in the doubly linked list so that the first node becomes the last node while the last node becomes the first node.

Search

Search operation in the doubly linked list is used to search for a particular node in the linked list. For this purpose, we need to traverse the list until a matching data is found.

Insertion

Insert a node at the front

Insertion of a new node at the front of the list is shown above. As seen, the previous new node N is set to null. Head points to the new node. The next pointer of N now points to N1 and previous of N1 that was earlier pointing to Null now points to N.

Insert node at the end

Inserting node at the end of the doubly linked list is achieved by pointing the next pointer of new node N to null. The previous pointer of N is pointed to N5. The ‘Next’ pointer of N5 is pointed to N.

Insert node before/after given node

As shown in the above diagram, when we have to add a node before or after a particular node, we change the previous and next pointers of the before and after nodes so as to appropriately point to the new node. Also, the new node pointers are appropriately pointed to the existing nodes.

The following C++ program demonstrates all the above methods to insert nodes in the doubly linked list.

#include using namespace std; // A doubly linked list node struct Node { int data; struct Node* next; struct Node* prev; }; //inserts node at the front of the list void insert_front[struct Node** head, int new_data] { //allocate memory for New node struct Node* newNode = new Node; //assign data to new node newNode->data = new_data; //new node is head and previous is null, since we are adding at the front newNode->next = [*head]; newNode->prev = NULL; //previous of head is new node if [[*head] != NULL] [*head]->prev = newNode; //head points to new node [*head] = newNode; } /* Given a node as prev_node, insert a new node after the given node */void insert_After[struct Node* prev_node, int new_data] { //check if prev node is null if [prev_node == NULL] { coutnext = prev_node->next; //set next of prev node to newnode prev_node->next = newNode; //now set prev of newnode to prev node newNode->prev = prev_node; //set prev of new node's next to newnode if [newNode->next != NULL] newNode->next->prev = newNode; } //insert a new node at the end of the list void insert_end[struct Node** head, int new_data] { //allocate memory for node struct Node* newNode = new Node; struct Node* last = *head; //set last node value to head //set data for new node newNode->data = new_data; //new node is the last node , so set next of new node to null newNode->next = NULL; //check if list is empty, if yes make new node the head of list if [*head == NULL] { newNode->prev = NULL; *head = newNode; return; } //otherwise traverse the list to go to last node while [last->next != NULL] last = last->next; //set next of last to new node last->next = newNode; //set last to prev of new node newNode->prev = last; return; } // This function prints contents of linked list starting from the given node void displayList[struct Node* node] { struct Node* last; while [node != NULL] { cout

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề