0% found this document useful (0 votes)
19 views10 pages

Doubly Linked List

The document provides a complete C++ implementation of a Doubly Linked List (DLL) with major operations including insertion, deletion, searching, and displaying the list in both forward and reverse directions. It includes detailed code for each operation and a main function demonstrating the usage of these operations. The code is structured with a Node class for individual elements and a DoublyLinkedList class to manage the list operations.

Uploaded by

awanakash83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Doubly Linked List

The document provides a complete C++ implementation of a Doubly Linked List (DLL) with major operations including insertion, deletion, searching, and displaying the list in both forward and reverse directions. It includes detailed code for each operation and a main function demonstrating the usage of these operations. The code is structured with a Node class for individual elements and a DoublyLinkedList class to manage the list operations.

Uploaded by

awanakash83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

All operations of Doubly LinkedList

Here is a complete C++ program implementing **Doubly Linked List (DLL)**


with all major operations, such as:

1. Insertion at the beginning, end, or after a specific node

2. Deletion from the beginning, end, or by value

3. Searching for an element

4. Displaying the list in forward and reverse directions

Code Implementation

#include <iostream>

using namespace std;

class Node {

public:

int data;

Node* prev;

Node* next;

// Constructor to initialize a new node

Node(int value) {

data = value;

prev = nullptr;

next = nullptr;

};

class DoublyLinkedList {

private:

Node* head;
public:

// Constructor to initialize an empty list

DoublyLinkedList() {

head = nullptr;

// 1. Insert at the beginning

void insertAtBeginning(int value) {

Node* newNode = new Node(value);

if (head != nullptr) {

head->prev = newNode;

newNode->next = head;

head = newNode;

// 2. Insert at the end

void insertAtEnd(int value) {

Node* newNode = new Node(value);

if (head == nullptr) {

head = newNode;

return;

Node* temp = head;

while (temp->next != nullptr) {

temp = temp->next;
}

temp->next = newNode;

newNode->prev = temp;

// 3. Insert after a specific node value

void insertAfter(int key, int value) {

Node* temp = head;

while (temp != nullptr && temp->data != key) {

temp = temp->next;

if (temp == nullptr) {

cout << "Node with value " << key << " not found.\n";

return;

Node* newNode = new Node(value);

newNode->next = temp->next;

newNode->prev = temp;

if (temp->next != nullptr) {

temp->next->prev = newNode;

temp->next = newNode;

// 4. Delete from the beginning

void deleteFromBeginning() {
if (head == nullptr) {

cout << "List is empty.\n";

return;

Node* temp = head;

head = head->next;

if (head != nullptr) {

head->prev = nullptr;

delete temp;

// 5. Delete from the end

void deleteFromEnd() {

if (head == nullptr) {

cout << "List is empty.\n";

return;

if (head->next == nullptr) {

delete head;

head = nullptr;

return;

Node* temp = head;

while (temp->next != nullptr) {


temp = temp->next;

temp->prev->next = nullptr;

delete temp;

// 6. Delete by value

void deleteByValue(int value) {

if (head == nullptr) {

cout << "List is empty.\n";

return;

if (head->data == value) {

deleteFromBeginning();

return;

Node* temp = head;

while (temp != nullptr && temp->data != value) {

temp = temp->next;

if (temp == nullptr) {

cout << "Value " << value << " not found in the list.\n";

return;

if (temp->next != nullptr) {
temp->next->prev = temp->prev;

if (temp->prev != nullptr) {

temp->prev->next = temp->next;

delete temp;

// 7. Search for a value

bool search(int value) {

Node* temp = head;

while (temp != nullptr) {

if (temp->data == value) return true;

temp = temp->next;

return false;

// 8. Display the list in forward direction

void displayForward() {

if (head == nullptr) {

cout << "List is empty.\n";

return;

Node* temp = head;


while (temp != nullptr) {

cout << temp->data << " <-> ";

temp = temp->next;

cout << "NULL\n";

// 9. Display the list in reverse direction

void displayReverse() {

if (head == nullptr) {

cout << "List is empty.\n";

return;

Node* temp = head;

while (temp->next != nullptr) {

temp = temp->next;

while (temp != nullptr) {

cout << temp->data << " <-> ";

temp = temp->prev;

cout << "NULL\n";

};
int main() {

DoublyLinkedList list;

list.insertAtBeginning(10);

list.insertAtEnd(20);

list.insertAtEnd(30);

list.insertAfter(20, 25);

cout << "List in forward direction: ";

list.displayForward();

cout << "List in reverse direction: ";

list.displayReverse();

list.deleteFromBeginning();

cout << "List after deleting from beginning: ";

list.displayForward();

list.deleteFromEnd();

cout << "List after deleting from end: ";

list.displayForward();

list.deleteByValue(25);

cout << "List after deleting value 25: ";

list.displayForward();
if (list.search(20)) {

cout << "Element 20 found in the list.\n";

} else {

cout << "Element 20 not found in the list.\n";

return 0;

Explanation of the Code

1. **Class `Node`**:

- Represents a node with data, a pointer to the previous node, and a pointer to the
next node.

2. **Class `DoublyLinkedList`**:

- Manages all operations on the doubly linked list.

3. **Insertion Methods**:

- **At the beginning**: Adds a new node at the start.

- **At the end**: Appends a new node at the end.

- **After a specific value**: Inserts a new node after a given node.

4. **Deletion Methods**:

- **Beginning**: Removes the first node.

- **End**: Removes the last node.

- **By value**: Deletes a node with the given value.

5. **Search Method**:
- Searches for a specific element in the list.

6. **Display Methods**:

- **Forward**: Displays nodes from head to tail.

- **Reverse**: Displays nodes from tail to head.

7. **Main Function**:

- Demonstrates the use of all operations.

Sample Output```

List in forward direction: 10 <-> 20 <-> 25 <-> 30 <-> NULL

List in reverse direction: 30 <-> 25 <-> 20 <-> 10 <-> NULL

List after deleting from beginning: 20 <-> 25 <-> 30 <-> NULL

List after deleting from end: 20 <-> 25 <-> NULL

List after deleting value 25: 20 <-> NULL

Element 20 found in the list.

```

---

This code provides all essential operations for a **Doubly Linked List** with
clear examples. You can further customize it for additional functionality, like
sorting or merging two lists.

You might also like