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:
Include necessary libraries: The code begins by including the
iostream
library, which allows input/output operations.Define the
Node
structure: The code defines a structure namedNode
, 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, andnext
to store a pointer to the next node.Define the
DoublyLinkedList
class: The code defines a class namedDoublyLinkedList
, which represents the doubly linked list. It has two private member variables:head
to store a pointer to the first node in the list, andtail
to store a pointer to the last node in the list.Define the constructor: The code defines a constructor for the
DoublyLinkedList
class. It initializes thehead
andtail
pointers tonullptr
, indicating an empty list.Define the
insert
function: The code defines a member function namedinsert
that takes an integer value as input. Inside the function, a new node is created using dynamic memory allocation (new Node
). Thedata
member of the new node is set to the input value, and theprev
andnext
pointers are set tonullptr
.Handle the case of an empty list: If the
head
pointer isnullptr
, indicating an empty list, thehead
andtail
pointers are set to the new node, as it is the only node in the list.Handle the case of a non-empty list: If the list is not empty, the
next
pointer of the currenttail
node is set to the new node, and theprev
pointer of the new node is set to the currenttail
. Then, thetail
pointer is updated to point to the new node, as it is now the last node in the list.Define the
display
function: The code defines a member function nameddisplay
that is used to print the values of the nodes in the list. It starts by initializing a temporary pointercurrent
to point to thehead
node.Traverse the list and print the values: The function enters a loop that continues as long as the
current
pointer is notnullptr
. Inside the loop, the value of the current node (current->data
) is printed, followed by a space. Then, thecurrent
pointer is updated to point to the next node in the list.Print a new line: After the loop, a new line is printed to separate the output.
Define the
main
function: The code defines themain
function, which serves as the entry point of the program. Inside themain
function, an instance of theDoublyLinkedList
class namedlist
is created.Insert values into the list: Three values (1, 2, and 3) are inserted into the
list
using theinsert
function.Display the list: The
display
function is called to print the values of the nodes in thelist
.Return 0: The
main
function ends by returning 0, indicating successful execution of the program.