Reversed Duplicate Linked List
#include <bits/stdc++.h> // Includes all standard librariesusing namespace std;
class Node {public: int data; // Stores data Node* next; // Pointer to next node Node* prev; // Pointer to previous node
Node(int data) { // Constructor to initialize node this->data = data; this->next = NULL; this->prev = NULL; }};
void printHead(Node* head) { if(head == NULL) { cout << "List is Empty!" << endl; return; }
cout << "Doubly List by Head : "; while(head != NULL) { cout << head->data << " "; // Traverse forward head = head->next; } cout << endl;}
void printTail(Node* tail) { if(tail == NULL) { cout << "List is Empty!" << endl; return; }
cout << "Doubly List by Tail : "; while(tail != NULL) { cout << tail->data << " "; // Traverse backward tail = tail->prev; } cout << endl;}
void insertAtHead(Node* &head, int data) { Node *temp = new Node(data); // Create new node temp->next = head; // Point to old head head->prev = temp; // Old head's prev is new node head = temp; // Update head}
void insertAtTail(Node* &tail, int data) { Node *temp = new Node(data); // Create new node tail->next = temp; // Old tail's next points to new node temp->prev = tail; // New node's prev points to old tail tail = temp; // Update tail}
void insertAtMiddle(Node* &head, Node* &tail, int data, int pos) { if(pos == 1) { insertAtHead(head, data); // Insert at head return; }
Node *temp = head; while(temp != NULL && pos>2) { temp = temp->next; pos--; }
if(temp==NULL || pos<=0) { cout << "Invalid Position!" << endl; return; }
Node *insertNode = new Node(data); insertNode->next = temp->next; insertNode->prev = temp; temp->next = insertNode;
if(insertNode->prev == tail) { // Update tail if inserted at end tail = insertNode; } else { temp->next->prev = insertNode; }}
void deletePosition(Node* &head, Node* &tail, int pos) { Node *temp = head; if(pos == 1) { head = head->next; delete temp;
if(head != NULL) { head->prev = NULL; } else { tail = NULL; }
return; }
while(temp!=NULL && pos>2) { temp = temp->next; pos--; }
if(temp==NULL || temp->next==NULL || pos<=0) { cout << "Invalid Position!" << endl; return; }
Node* target = temp->next; temp->next = target->next;
if(target == tail) { tail = tail->prev; } else { target->next->prev = temp; }
delete target;}
void deleteValue(Node* &head, Node* &tail, int val) { Node* temp = head;
if(head->data == val) { head = head->next; delete temp;
if(head != NULL) { head->prev = NULL; } else { tail = NULL; }
return; }
while(temp!=NULL) { if(temp->data == val) { val = INT_MIN; break; }
temp = temp->next; }
if(temp==NULL || val!=INT_MIN) { cout << "Value Not Found!" << endl; return; }
temp->prev->next = temp->next;
if(temp == tail) { tail = tail->prev; } else { temp->next->prev = temp->prev; }
delete temp;}
void reverseDLL(Node* &head, Node* &tail) { Node* curr = head;
while(curr != NULL) { Node* temp = curr->prev; curr->prev = curr->next; // Swap next and prev curr = curr->next;
if(curr == NULL) { tail->next = temp; } else { curr->prev->next = temp; } }
curr = head; head = tail; tail = curr;}
int main() { Node *head = new Node(1); // Initial node Node *tail = head;
for(int i=1; i<=5; i++) { insertAtTail(tail, pow(2,i)); // Add 2, 4, 8, 16, 32 }
printHead(head); // Print forward printTail(tail); // Print backward
reverseDLL(head, tail); // Reverse list
printHead(head); printTail(tail);
return 0;}
Diagram (Initial list):
Head → 1 ⇄ 2 ⇄ 4 ⇄ 8 ⇄ 16 ⇄ 32 ← Tail
After reverseDLL:
Head → 32 ⇄ 16 ⇄ 8 ⇄ 4 ⇄ 2 ⇄ 1 ← Tail