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 != 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 < 0)
return false;
unsigned int ctr = 0;
Node *ptr = head;
while (ptr != nullptr && ctr < index)
{
ptr = ptr->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 < count; i++)
{
AddHead(arr[i]);
length++;
}
}
void AddNodesTail(const T arr[], const unsigned int count)
{
for (unsigned int i = 0; i < count; i++)
{
AddTail(arr[i]);
length++;
}
}
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 <= back)
{
if (front == node)
{
ptr = front;
break;
}
else if (back == node)
{
ptr = back;
break;
}
front = 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 <= back)
{
if (front == node)
{
ptr = front;
break;
}
else if (back == node)
{
ptr = back;
break;
}
front = 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 < index && ptr != nullptr;
i++)
{
ptr = ptr->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 <= back)
{
if (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 < index; i++)
{
ptr = ptr->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<Node*>& 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 < index; i++)
{
ptr = ptr->next;
}
return ptr->data;
}
private:
unsigned int length;
Node* head, *tail;
};
#endif
//MODIFY AddHead AS:-
void AddHead(const T data)
{
if(head==nullptr)
{
head=new Node;
head->data=data;
tail=head;
head->prev=head->next=tail->prev=tail->next=nullptr;
}
else
{
Node *new_node=new Node;
new_node->data=data;
new_node->next=head;
head->prev=new_node;
head=new_node;
}
length++;
}
//MODIFY AddTail As:-
void AddTail(const T data)
{
if(tail==nullptr)
{
tail=new Node;
tail->data=data;
head=tail;
head->prev=head->next=tail->prev=tail->next=nullptr;
}
else
{
Node *new_node=new Node;
new_node->data=data;
new_node->prev=tail;
tail->next=new_node;
tail=new_node;
}
length++;
}
//Rest seems to be correct.If any more problem,please comment
The program I wrote has a few memory leaks. My assignment is to just fix the...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...
In this assignment, you will implement a sort method on
singly-linked and doubly-linked lists.
Implement the following sort member function on a singly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
Implement the following sort member function on a doubly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
The sort(…) methods take as a parameter a comparator function,
having a default assignment of defaultCompare, a
static function defined as follows:
template <typename T>
static bool defaultCompare(const...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...
Fill the code in list.cpp app.cpp: #include "list.hpp" int main() { LinkedList aList; bool success = false; aList.evensFrontOddsEnd(3); aList.evensFrontOddsEnd(10); aList.evensFrontOddsEnd(6); aList.evensFrontOddsEnd(9); aList.evensFrontOddsEnd(17); aList.evensFrontOddsEnd(12); aList.printForward(); // output should be: 12 6 10 3 9 17 aList.printBackward(); // output should be: 17 9 3 10 6 12 success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl; success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...
Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public: ItemType(); void setValue(int newValue); int getValue() const; RelationType ComparedTo(ItemType newItem); private: int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() { value = 0; } void ItemType::setValue(int newValue) { value = newValue; } int ItemType::getValue() const { return value; } RelationType ItemType::ComparedTo(ItemType newItem)...
Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...
C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...
I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...
I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be: H create link at head R remove link at head T create link at tail K remove link at tail I remove link at ID S search...