Question: The program I wrote has a few memory leaks. My assignment is to just fix the memory leaks. Here's the code: bool RemoveTail() { if

The program I wrote has a few memory leaks. My assignment is to just fix the memory leaks. Here's the code:

The program I wrote has a few memory leaks. My assignment is

to just fix the memory leaks. Here's the code: bool RemoveTail() {

if (tail == nullptr) return false; Node *ptr = tail; if(tail->prev !=

bool RemoveTail() { if (tail == nullptr) return false; Node *ptr = tail; if(tail->prev != nullptr) { tail = tail->prev; tail->next = nullptr; } else { tail = nullptr; head = nullptr; } delete ptr; ptr = nullptr; length--; return true; }

bool RemoveAt(const unsigned int index) { if(index >= length || index

unsigned int ctr = 0; Node *ptr = head; while (ptr != nullptr && ctr next; ctr++; } if (ptr == nullptr) { return false; } else { if(ptr->prev != nullptr) ptr->prev->next = ptr->next; if(ptr->next != nullptr) ptr->next->prev = ptr->prev; delete ptr; ptr = nullptr; length--; return true; } }

void AddNodesHead(const T arr[], const unsigned int count) { for (unsigned int i = 0; i

void AddNodesTail(const T arr[], const unsigned int count) { for (unsigned int i = 0; i

void InsertBefore(Node *node, const T data) { Node *front = head, *back = tail; Node *ptr; if (front == nullptr && back == nullptr) { AddHead(data); return; } if (node == nullptr) return; while (front next; back = back->prev; } Node *new_node = new Node; new_node->data = data; ptr->prev->next = new_node; new_node->prev = ptr->prev; new_node->next = ptr; ptr->prev = new_node; length++; }

void InsertAfter(Node *node, const T data) { Node *front = head, *back = tail; Node *ptr = nullptr; if (front == nullptr && back == nullptr) AddHead(data); if (node == nullptr) return; while (front next; back = back->prev; } Node *new_node = new Node; new_node->data = data; ptr->next->prev = new_node; new_node->next = ptr->next; new_node->prev = ptr; ptr->next = new_node; length++; }

Node* GetNode(const unsigned int index) { Node *ptr = head; for (unsigned int i = 0; i next; } return ptr; } Node* Find(const T val) { Node *front = head, *back = tail; Node *ptr = nullptr; if (front == nullptr && back == nullptr) { return nullptr; } while (front data == val) { ptr = front; break; } else if (back->data == val) { ptr = back; break; } front = front->next; back = back->prev; } return ptr; }

void InsertAt(const T data, const unsigned int index) { Node *ptr = head; if (index > length) return; else if (index == 0) AddHead(data); else if (index == length - 1) AddTail(data); else { for (unsigned int i = 0; i next; } Node *new_node = new Node; ptr->prev->next = new_node; new_node->prev = ptr->prev; new_node->next = ptr; ptr->prev = new_node; } }

void FindAll(std::vector& nodes, const T search_val) { Node *ptr = head; while(ptr != nullptr) { if(ptr->data == search_val) nodes.push_back(ptr); ptr = ptr->next; } }

T& operator[](const unsigned int index) { Node *ptr = head; for (unsigned int i = 0; i next; } return ptr->data; }

private: unsigned int length; Node* head, *tail;

}; #endif

#ifndef LINKEDLIST H #define LINKEDLIST.H #include template class LinkedList public: class Node public: Node) this->next-this->prev = nullptr; //this->data = 0; T data; Node *next, *prev; LinkedLis head = tail = nullptr; length = 0; void AddHead(const T data) if (head == nullptr) head = new Node; head->data data; tail = head; else Node *new-node new Node; new-node->data = data; new-node->next = head; head->prev = new node; head head->prev length++

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!