🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Doubly linked lists are an extension of the simple linked list, where each node points to both its next node and its previous node. This bidirectional linkage allows for easier traversal in both forward and backward directions. In this blog post, we will create a basic doubly linked list in C++ and implement basic operations like insertion, deletion, and display.
2. Program Overview
Our program will:
1. Define a Node structure for the doubly linked list.
2. Implement a DoublyLinkedList class with functions to:
- Add elements to the end of the list.
- Remove elements from the list.
- Display elements in the list in both forward and reverse order.
3. Code Program
#include<iostream>
using namespace std;
// Node structure for Doubly Linked List
class Node {
public:
int data;
Node* prev;
Node* next;
// Constructor to initialize a new node
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
// Function to append a new node at the end
void append(int data) {
Node* newNode = new Node(data);
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next) {
temp = temp->next; // Traverse to the last node
}
temp->next = newNode;
newNode->prev = temp;
}
}
// Function to delete a node by value
void deleteNode(int value) {
Node* temp = head;
while (temp) {
if (temp->data == value) {
if (temp->prev) temp->prev->next = temp->next;
if (temp->next) temp->next->prev = temp->prev;
if (temp == head) head = head->next;
delete temp;
return;
}
temp = temp->next;
}
}
// Function to display the list forward
void displayForward() {
Node* temp = head;
while (temp) {
cout << temp->data << " <-> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
// Function to display the list in reverse
void displayReverse() {
Node* temp = head;
while (temp && temp->next) {
temp = temp->next; // Move to the last node
}
while (temp) {
cout << temp->data << " <-> ";
temp = temp->prev;
}
cout << "NULL" << endl;
}
};
int main() {
DoublyLinkedList dll;
dll.append(10);
dll.append(20);
dll.append(30);
cout << "Doubly Linked List forward: ";
dll.displayForward();
cout << "Doubly Linked List reverse: ";
dll.displayReverse();
dll.deleteNode(20);
cout << "After deletion of 20: ";
dll.displayForward();
return 0;
}
Output:
Doubly Linked List forward: 10 <-> 20 <-> 30 <-> NULL Doubly Linked List reverse: 30 <-> 20 <-> 10 <-> NULL After deletion of 20: 10 <-> 30 <-> NULL
4. Step By Step Explanation
1. We've defined a Node class which represents an element in our doubly linked list, with pointers to the next and previous nodes.
2. Our DoublyLinkedList class provides functions to append data to the list, delete nodes, and display the list in both directions.
3. In the append method, if the list is empty, the new node becomes the head. Otherwise, it's attached at the end.
4. The deleteNode method searches for a node by value and adjusts the pointers of the adjacent nodes before deleting it.
5. We've also provided a demonstration in the main function, where we create a list, display it, and then delete a node.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment