Question
Please convert the following class and struct and the two member functions to be templates/template compatible struct Node { Point xy; Node* next; Node* prev;
Please convert the following class and struct and the two member functions to be templates/template compatible
struct Node { Point xy; Node* next; Node* prev; }; class DLinkedList { public: Node* head; Node* tail; DLinkedList() : head(nullptr), tail(nullptr) {} void addNode(Node*); // add a Node to the list Node* removeNode(Point); //remove a Node from the list }; void DLinkedList::addNode(Node* node) { if(head == nullptr){ //List is empty, (head=tail) head = node; tail = head; } else{ // Add node to end of list (make node become tail) tail->next = node; node->prev = tail; tail = node; }
// Fill in the missing code with the assumption that a new Node is added to the end // of the list, which involves using the current tail pointer value to access the previous // Node to update its next pointer value to point to the newly added Node, and then // updating the tail pointer to also point to the newly added Node. CAREFUL to deal // correctly with an empty list (i.e. both head and tail pointers are nullptr). } Node* DLinkedList::removeNode(Point XY) {
Node* node = nullptr; if(head->xy == XY){ //point desired for deletion is the head node = head; head = head->next; head->prev=nullptr; return node; } else if(tail->xy == XY){ //point desired for deletion is the tail node=tail; tail=tail->prev; tail->next=nullptr; return node; } Node* temp = head; while(temp != tail){ //Go through all nodes if(temp->xy==XY){ //Find node with point desired for deletion node=temp; Node* pre= temp->prev; //close the gap left by the deleted node by linking the surrounding nodes Node* post=temp->next; pre->next=post; post->prev=pre; break; } temp=temp->next; //Get next node in list } return node; //Return node that was removed }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started