diff --git a/Data Structures/DoublyLinkedList.cpp b/Data Structures/DoublyLinkedList.cpp new file mode 100644 index 0000000..6cb5f52 --- /dev/null +++ b/Data Structures/DoublyLinkedList.cpp @@ -0,0 +1,206 @@ +// Doubly Linked List +#include +using namespace std; +class Node +{ +public: + int data; + Node *next; + Node *prev; + Node(int data) + { + this->data = data; + this->next = NULL; + this->prev = NULL; + } + ~Node() + { + int value = this->data; + // memory free message + if (this->next != NULL) + { + delete next; + this->next = NULL; + } + cout << "Memory is free for node with data " << value << endl; + } +}; +void print(Node *head, Node *tail) +{ + Node *temp = head; + while (temp != NULL) + { + cout << temp->data << " "; + temp = temp->next; + } + cout << endl; + cout << "head = " << head->data << " and tail = " << tail->data << endl + << endl; +} + +int getLength(Node *head) +{ + int length = 0; + Node *temp = head; + while (temp != NULL) + { + length++; + temp = temp->next; + } + return length; +} + +void insertAtHead(Node *&tail, Node *&head, int data) +{ + if (head == NULL) + { + Node *temp = new Node(data); + head = temp; + tail = temp; + } + else + { + Node *temp = new Node(data); + temp->next = head; + head->prev = temp; + head = temp; + } +} + +void insertAtTail(Node *&tail, Node *&head, int data) +{ + if (tail == NULL) + { + Node *temp = new Node(data); + head = temp; + tail = temp; + } + + Node *temp = new Node(data); + tail->next = temp; + temp->prev = tail->next; + tail = temp; +} + +void insertAtPosition(Node *&tail, Node *&head, int position, int d) +{ + if (position == 1) + { + insertAtHead(tail, head, d); + return; + } + Node *temp = head; + int cnt = 1; + while (cnt < position - 1) + { + temp = temp->next; + cnt++; + } + + // inserting at last position + if (temp->next == NULL) + { + insertAtTail(tail, head, d); + return; + } + + // creating a node for d + Node *nodeToInsert = new Node(d); + + nodeToInsert->next = temp->next; + temp->next->prev = nodeToInsert; + temp->next = nodeToInsert; + nodeToInsert->prev = temp; +} + +void deleteNode(int position, Node *&head) +{ + + if (position == 1) + + { + Node *temp = head; + temp->next->prev = NULL; + head = temp->next; + temp->next = NULL; + delete temp; + } + else + { + Node *curr = head; + Node *prev = NULL; + int count = 1; + while (count < position) + { + prev = curr; + curr = curr->next; + count++; + } + // Order is important + curr->prev = NULL; + prev->next = curr->next; + curr->next = NULL; + /* + +Using curr->next->prev = NULL; is incorrect because it breaks the backward link +of the doubly linked list. You should instead use curr->next->prev = prev; to ensure +the list remains correctly linked. + + +chatgpt + +5 <-> 10 <-> 15 <-> 20 <-> 25 + ^ ^ ^ ^ + prev curr next + +prev->next = curr->next; // 10.next = 20 +curr->next->prev = prev; // 20.prev = 10 +curr->next = NULL; // 15.next = NULL +curr->prev = NULL; // 15.prev = NULL + +5 <-> 10 <-> 20 <-> 25 + ^ ^ + prev next + +*/ + + delete curr; + } +} + +int main() +{ + Node *node1 = new Node(10); + Node *head = node1; + Node *tail = node1; + print(head, tail); + + insertAtHead(tail, head, 15); + print(head, tail); + + insertAtPosition(tail, head, 2, 29); + print(head, tail); + + deleteNode(2, head); + print(head, tail); + + // Node *head = NULL; to handle these we made those if else + // Node *tail = NULL; + // print(head); + + // cout << "Length of Doubly Linkedlist is " << getLength(head) << endl; + + /*insertAtHead(tail, head, 15); + print(head,tail); + + insertAtTail(tail, head, 20); + print(head,tail); + + insertAtPosition(tail, head, 3, 29); + print(head,tail); + + deleteNode(1, head); + print(head,tail);*/ + + return 0; +} \ No newline at end of file