If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Below is the C++ implementation of the
DoublyLinkedListclass with methods to insert elements at the head, tail, and at a specific position in the doubly linked list:cppCopy code
#include <iostream> class Node { public: int data; Node* next; Node* prev; Node(int data) : data(data), next(nullptr), prev(nullptr) {} }; class DoublyLinkedList { private: Node* head; Node* tail; public: DoublyLinkedList() : head(nullptr), tail(nullptr) {} ~DoublyLinkedList() { Node* current = head; while (current) { Node* temp = current; current = current->next; delete temp; } } void insertAtHead(int data) { Node* newNode = new Node(data); if (!head) { head = newNode; tail = newNode; } else { newNode->next = head; head->prev = newNode; head = newNode; } } void insertAtTail(int data) { Node* newNode = new Node(data); if (!tail) { head = newNode; tail = newNode; } else { newNode->prev = tail; tail->next = newNode; tail = newNode; } } void insertAtPosition(int data, int position) { if (position <= 0) { insertAtHead(data); } else if (!head) { std::cout << "List is empty. Cannot insert at position." << std::endl; } else { Node* newNode = new Node(data); Node* current = head; int count = 0; while (count < position - 1 && current->next) { current = current->next; count++; } if (count == position - 1) { newNode->prev = current; newNode->next = current->next; if (current->next) { current->next->prev = newNode; } else { tail = newNode; } current->next = newNode; } else { std::cout << "Invalid position. Insertion failed." << std::endl; delete newNode; } } } void displayForward() { if (!head) { std::cout << "List is empty." << std::endl; return; } Node* current = head; while (current) { std::cout << current->data << " -> "; current = current->next; } std::cout << "NULL" << std::endl; } void displayBackward() { if (!tail) { std::cout << "List is empty." << std::endl; return; } Node* current = tail; while (current) { std::cout << current->data << " -> "; current = current->prev; } std::cout << "NULL" << std::endl; } }; int main() { DoublyLinkedList list; list.insertAtHead(1); list.insertAtHead(2); list.insertAtTail(3); list.insertAtPosition(4, 1); list.displayForward(); // Output: 2 -> 4 -> 1 -> 3 -> NULL list.displayBackward(); // Output: 3 -> 1 -> 4 -> 2 -> NULL return 0; }This implementation of the
DoublyLinkedListclass contains the following methods:
insertAtHead: Inserts a new node at the beginning of the doubly linked list.insertAtTail: Inserts a new node at the end of the doubly linked list.insertAtPosition: Inserts a new node at the specified position in the doubly linked list. The position is 0-based, meaning the first element has position 0.The
displayForwardanddisplayBackwardmethods are used to display the elements of the doubly linked list in the forward and backward directions, respectively.In the
mainfunction, we demonstrate how to use theDoublyLinkedListclass by creating a doubly linked list, inserting elements at the head, tail, and at a specific position, and displaying the doubly linked list in both forward and backward directions. The output will be2 -> 4 -> 1 -> 3 -> NULLfor forward direction and3 -> 1 -> 4 -> 2 -> NULLfor backward direction after the insertions.
Start the conversation!
Be the first to share your thoughts
Quick answers to common questions about our courses, quizzes, and learning platform
Didn't find what you're looking for?
Contact Support