Site icon Techolac – Computer Technology News

Understand Doubly Linked List C++ With Illustration

doubly linked list c++

This post will explain doubly linked list c++. A twice as connected list is a variation of the singly linked list. We understand that the singly linked list is a collection of nodes, with each node having a data part and a pointer indicating the next node.

A doubly connected list is also a collection of nodes. Each node here includes an information part and two pointers. One tip points to the previous node, while the second pointer indicates the next node.

Understand Doubly Linked List C++ With Illustration

In this article, you can know about doubly linked list c++ here are the details below;

Doubly Linked In C++

As in the singly connected list, the two times as connected list also has a head and a tail. The previous guideline of the head is set to NULL as this is the very first node. The next pointers of the tail node is set to NULL as this is the last node.
A basic layout of the two times as connected list is displayed in the below diagram.

In the above figures, we see that each node has two pointers, pointing to the previous node and the other indicating the next node. Only the primary node (head) has its previous nodes set to null, and the last node (tail) has its next pointers set to null. You can also check best ihome speakers.

As the doubly connected list contains two pointers, i.e., previous and next, we can traverse it into the instructions forward and backward. This is the primary benefit of two times as a linked list over the singly connected list.

Declaration

In C-style declaration, a nodes of the doubly linked list is represented as follows:
struct node
;
Apart from the above declaration, we can likewise represent a node in the doubly linked list as a classes in C++. A doubly connected list is represented as a class when we utilize STL in C++. We can execute a twice as linked list using a class in Java too.

Basic Operations

Following are some of the operation’s that we can perform two times as a connected list.
Insertion
Insertion operation of the twice as connected list inserts a brand-new node in the linked list. Depending opon the position where the new node is to be placed, we can have the following

insert operations.

– Insertion at the front– Inserts a brand-new node as the first node.
– Insertion at the end– Inserts a brand-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 provided position in the twice as connected list
– Deletion of the first node– Deletes the very first node in the list.
– Deletion of the last node– Deletes the last node in the list.
Deleting a node given the information: Given the information, the operation matches the data with the node information in the linked list and deletes that node.

Traversal

Traversal is a strategy of going to each node in the connected list. In a twice as connected list, we have two kinds of traversals as we have two guidelines with various instructions in the two times as linked list.
– Forward Traversal– Traversal is done using the next tip, which is in the forward instructions.
– Backward Traversal– Traversal is done utilizing the previous guideline, which is the backward direction.

Reverse

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

Search

Search operation in the doubly connected list is used to look for a specific node in the linked list. For this purpose, we require to traverse the list until matching information is found.

Insertion

Insert a node at the front

The insertion of a new nodes at the front of the list is shown above. As seen, the previous brand-new node N is set to null. Head points to the new node. The next tip now indicates N1, and the previous of N1 that was earlier pointing to Null now indicates N.
Insert node at the end

Inserting a nodes at the end of the two times as linked list is attained by pointing the next node N to null. The premature pointer of N is pointed to N5. The ‘Next’ tip of N5 is pointed to N.

Insert node before/after given node

As shown in the aboves diagram, when we need to include a node before or after a particular node, we alter the previous and next guidelines of the before and after nodes to properly indicate the new node. Likewise, the brand-new node guidelines are appropriately pointed to the existing nodes. Also check textsheet alternatives.

The following C++ program shows all the above methods to insert nodes in the twice as linked list.

#include <iostream>
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) {
   cout<<"Previous node is required , it cannot be NULL";
   return;
}
   //allocate memory for new node
   struct Node* newNode = new Node;
 
   //assign data to new node
   newNode->data = new_data;
 
   //set next of newnode to next of prev node
   newNode->next = 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<<node->data<<"<==>";
      last = node;
      node = node->next;
   }
   if(node == NULL)
   cout<<"NULL";
   }
 
//main program
int main() {
   /* Start with the empty list */
   struct Node* head = NULL;
 
   // Insert 40 as last node
   insert_end(&head, 40);
 
   // insert 20 at the head
   insert_front(&head, 20);
 
   // Insert 10 at the beginning.
   insert_front(&head, 10);
 
   // Insert 50 at the end.
   insert_end(&head, 50);
 
   // Insert 30, after 20.
   insert_After(head->next, 30);
 
   cout<<"Doubly linked list is as follows: "<<endl;
   displayList(head);
   return 0;
}

A node can be erased from a twice as connected list from any position like the front, end, or any other offered position or offered information.
When deleting a node from the doubly connected list, we first reposition the pointer indicating that particular node. The previous and after nodes don’t have any connection to the node to be deleted. We can then quickly erase the node.

Consider the following two times as a connected list with three nodes A, B, C. Let us consider that we require to erase node B.

As displayed in the diagram above, we have demonstrated the deletion of node B from the provided connected list. The series of operations remains the same even if the node is 1st or last. The only care that should be considered is that if the first node is erased, the second node’s previous pointer will be null.

Likewise, when the last node is deleted, the previous node’s next pointer will be set to null. If in-between nodes are deleted, then the sequence will be as above.
We leave the programs to delete a node from two times as a linked list. Note thats the implementation will be on the line’s of the insertion implementation. Also check wow private servers.

Reverse Doubly Linked List.

Reversing two times as a linked list is a crucial operation. In this, we just swap the previous and next tips of all the nodes and likewise switch the head and tail guidelines.
Provided listed below is a doubly connected list:

// Java Class for Doubly Linked List
class Doubly_linkedList {
   Node head; // list head
 
    /* Doubly Linked list Node*/
   class Node {
      int data;
      Node prev;
      Node next;
 
      //create a new node using constructor
      Node(int d) { data = d; }
   }
 
// insert a node at the front of the list
public void insert_front(int new_data)
{
   /* 1. allocate node
   * 2. put in the data */
   Node new_Node = new Node(new_data);
 
   /* 3. Make next of new node as head and previous as NULL */
   new_Node.next = head;
   new_Node.prev = null;
 
   /* 4. change prev of head node to new node */
   if (head != null)
   head.prev = new_Node;
 
   /* 5. move the head to point to the new node */
   head = new_Node;
}
//insert a node after the given prev node
public void Insert_After(Node prev_Node, int new_data)
{
   //check that prev node is not null
   if (prev_Node == null) {
   System.out.println("The previous node is required,it cannot be NULL ");
   return;
}
 
   //allocate new node and set it to data
   Node newNode = new Node(new_data);
 
   //set next of newNode as next of prev node
   newNode.next = prev_Node.next;
 
   //set new node to next of prev node
   prev_Node.next = newNode;
 
   //set prev of newNode as prev node
   newNode.prev = prev_Node;
 
   //set prev of new node's next to newnode
   if (newNode.next != null)
   newNode.next.prev = newNode;
}
 
// Add a node at the end of the list
void insert_end(int new_data)   {
   //allocate the node and set the data
   Node newNode = new Node(new_data);
   Node last = head; //set last as the head
   //set next of new node to null since its the last node
   newNode.next = null;
 
   //set new node as head if the list is null
   if (head == null) {
      newNode.prev = null;
      head = newNode;
      return;
   }
   //if list is not null then traverse it till the last node and set last next to last
   while (last.next != null)
   last = last.next;
 
   last.next = newNode;           //set last next to new node
 
   newNode.prev = last;           //set last as prev of new node
}
// display the contents of linked list starting from the given node
public void displaylist(Node node)
{
   Node last = null;
   while (node != null) {
      System.out.print(node.data + "<==>");
      last = node;
      node = node.next;
   }
   if(node == null)
   System.out.print("null");
   System.out.println();
 
   }
}
class Main{
   public static void main(String[] args)
      {
      /* Start with the empty list */
      Doubly_linkedList dll = new Doubly_linkedList();
 
      // Insert 40.
      dll.insert_end(40);
 
      // Insert 20 at the beginning.
      dll.insert_front(20);
 
      // Insert 10 at the beginning.
      dll.insert_front(10);
 
      // Insert 50 at the end.
      dll.insert_end(50);
 
      // Insert 30, after 20.
      dll.Insert_After(dll.head.next, 30);
 
      System.out.println("Doubly linked list created is as follows: ");
      dll.displaylist(dll.head);
      }
}

Benefits:

– The doubly linked list can be traverseds in forward and backward directions, unlike singly connected list which can be passed through in the forward direction just.
– Delete operation in a doubly-linked lists is more efficient when compared to a singly note when a provided node is given. As we require a previous node to erase the provided node in a singly linked list, we often need to traverse the list to discover the previous node. This hits the performance.
– Insertion operation can be done quickly in two times as linked to the singly connected list.

Exit mobile version