Question

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

#ifndef LINKEDLIST H #define LINKEDLIST.H #include <vector» #include <iostream> template<typename T> class LinkedList public: class Node public: Node) this->next-this->prev = nullptr; //this->data = 0; T data; Node *next, *prev; LinkedLis<T> 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++;


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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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

Add a comment
Know the answer?
Add Answer to:
The program I wrote has a few memory leaks. My assignment is to just fix the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    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...

    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...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    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...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    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...

    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;...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    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...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

  • Hi I need a fix in my program. The program needs to finish after serving the...

    Hi I need a fix in my program. The program needs to finish after serving the customers from the queue list. Requeriments: Headers: DynamicArray.h #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class DynamicArray { V* values; int cap; V dummy; public: DynamicArray(int = 2); DynamicArray(const DynamicArray&); ~DynamicArray() { delete[] values; } int capacity() const { return cap; } void capacity(int); V operator[](int) const; V& operator[](int); DynamicArray& operator=(const DynamicArray&); }; template DynamicArray::DynamicArray(int cap) { this->cap = cap; values =...

  • Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...

    Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Starter testDeque.cpp which contains: Timer class holder (you need to go through the LearnCpp Ch15 and import it in) Node class Deque class specification (you need to fill out the definition) here is the testDeque.cpp code: // C++ implementation of doubly linked list Deque doubly linked list...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT