double linked list
Doubly Linked Lists are a type of sequential data structure that differs from a singly linked list in that each node contains two pointers: one pointing to the next node and the other pointing to the previous node.
In a doubly linked list:
- Each node has two pointers: one pointing to the next node in the list and the other pointing to the previous node.
- The "prev" pointer of the head node is NULL since there is no previous node before the head.
- The "next" pointer of the last node in the list is NULL because there is no node after the last node.
These properties of doubly linked lists enable efficient traversal in both directions (forward and backward) and provide flexibility for various operations on the list, such as insertion and deletion at both ends.
Sample of double linked list node in C++:
struct Node {
int data;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in DLL
};
Sample of double-linked list node in Java:
class Node {
int data;
Node prev;
Node next;
// Constructor to create a new node
// next and prev is by default initialized as null
Node(int d) { data = d; }
}
Advantages of doubly-linked lists over singly-linked lists:
Bidirectional traversal: A doubly linked list can be traversed in both forward and backward directions. This allows for more flexible navigation within the list compared to singly linked lists, which can only be traversed in one direction.
Efficient delete operation: When a pointer to the node to be deleted is given, the delete operation in a doubly linked list is more efficient. This is because, in a singly linked list, deleting a node requires knowledge of the previous node, which may involve traversing the list. In a doubly linked list, the previous node can be accessed directly through the previous pointer, resulting in faster deletion.
Easy insertion before a given node: Inserting a new node before a given node is quick and efficient in a doubly linked list. With the help of the previous pointer, the new node can be easily inserted before the desired node without the need for extensive traversal.
Disadvantages of doubly-linked lists over singly-linked lists:
Extra space requirement: Each node in a doubly linked list requires additional memory to store the previous pointer. This increases the overall space consumption compared to singly linked lists, which only require a single next pointer for each node.
Maintenance of additional pointers: All operations on a doubly linked list, such as insertion and deletion, require the additional pointer for the previous node to be maintained. This adds complexity and overhead to the list operations compared to singly linked lists.
Creating and traversing a doubly linked list:
Is a process similar to that of singly linked lists, with the additional requirement of maintaining a previous pointer for each node during list creation.class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
Node head;
public DoublyLinkedList() {
this.head = null;
}
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}
}
public void display() {
Node current = head;
System.out.println("Doubly Linked List:");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
// Inserting nodes in the doubly linked list
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
// Displaying the doubly linked list
list.display();
}
}
Doubly Linked List:
1 2 3 4 5
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int data) {
this->data = data;
this->prev = nullptr;
this->next = nullptr;
}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() {
this->head = nullptr;
}
void insert(int data) {
Node* newNode = new Node(data);
if (head == nullptr) {
head = newNode;
}
else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
void display() {
Node* current = head;
cout << "Doubly Linked List:" << endl;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
DoublyLinkedList list;
// Inserting nodes in the doubly linked list
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
// Displaying the doubly linked list
list.display();
return 0;
}
No comments