doubly linked list c++ code

#include <iostream>

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

class DoublyLinkedList {
private:
    Node* head;
    Node* tail;
public:
    DoublyLinkedList() {
        head = nullptr;
        tail = nullptr;
    }

    void insert(int value) {
        Node* newNode = new Node;
        newNode->data = value;
        newNode->prev = nullptr;
        newNode->next = nullptr;

        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }

    void display() {
        Node* current = head;
        while (current != nullptr) {
            std::cout << current->data << " ";
            current = current->next;
        }
        std::cout << std::endl;
    }
};

int main() {
    DoublyLinkedList list;

    list.insert(1);
    list.insert(2);
    list.insert(3);

    list.display();

    return 0;
}

Explanation:

  1. Include necessary libraries: The code begins by including the iostream library, which allows input/output operations.

  2. Define the Node structure: The code defines a structure named Node, which represents a single node in the doubly linked list. It has three members: data to store the value of the node, prev to store a pointer to the previous node, and next to store a pointer to the next node.

  3. Define the DoublyLinkedList class: The code defines a class named DoublyLinkedList, which represents the doubly linked list. It has two private member variables: head to store a pointer to the first node in the list, and tail to store a pointer to the last node in the list.

  4. Define the constructor: The code defines a constructor for the DoublyLinkedList class. It initializes the head and tail pointers to nullptr, indicating an empty list.

  5. Define the insert function: The code defines a member function named insert that takes an integer value as input. Inside the function, a new node is created using dynamic memory allocation (new Node). The data member of the new node is set to the input value, and the prev and next pointers are set to nullptr.

  6. Handle the case of an empty list: If the head pointer is nullptr, indicating an empty list, the head and tail pointers are set to the new node, as it is the only node in the list.

  7. Handle the case of a non-empty list: If the list is not empty, the next pointer of the current tail node is set to the new node, and the prev pointer of the new node is set to the current tail. Then, the tail pointer is updated to point to the new node, as it is now the last node in the list.

  8. Define the display function: The code defines a member function named display that is used to print the values of the nodes in the list. It starts by initializing a temporary pointer current to point to the head node.

  9. Traverse the list and print the values: The function enters a loop that continues as long as the current pointer is not nullptr. Inside the loop, the value of the current node (current->data) is printed, followed by a space. Then, the current pointer is updated to point to the next node in the list.

  10. Print a new line: After the loop, a new line is printed to separate the output.

  11. Define the main function: The code defines the main function, which serves as the entry point of the program. Inside the main function, an instance of the DoublyLinkedList class named list is created.

  12. Insert values into the list: Three values (1, 2, and 3) are inserted into the list using the insert function.

  13. Display the list: The display function is called to print the values of the nodes in the list.

  14. Return 0: The main function ends by returning 0, indicating successful execution of the program.