Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Hi!! I am having some issues creating this linked list. I am receiving errors specifically about memory leaks. I have written a few parts already in the LinkedList.h. I still need the...

1 answer below »
Hi!! I am having some issues creating this linked list. I am receiving errors specifically about memory leaks. I have written a few parts already in the LinkedList.h. I still need the following:Head(), Tail(), GetNode, Operator[](), Find, FindAll, the copy constructor, the copy assignment operator, InsertBefore(), InsertAfter(), InsertAt(), operator==, RemoveHead(), RemoveTail(), RemoveAt(), Remove(), Clear(), PrintForwardRecursive, PrintReverseRecursive.
In addition to the my issue with the memory leak
Answered 2 days After Oct 05, 2021

Solution

Arun Shankar answered on Oct 08 2021
142 Votes
LinkedList.cpp
#include "LinkedList.h"
template LinkedList::LinkedList(const LinkedList &list) {
this -> head = nullptr;
this -> tail = nullptr;
this -> count = 0;
Node* cu
= list.head;
while(cu
!= nullptr) {
AddTail(cu
-> data);
cu
= cu
-> next;
}
}
template LinkedList::~LinkedList() {
Node *cu
= head;
while (cu
!= nullptr) {
Node *temp = cu
-> next;
delete cu
;
cu
= temp;
}
head = nullptr;
}
template void LinkedList::AddHead(const T& data)
{
Node* temp = new Node;
temp -> data=data;
temp -> next=nullptr;
temp -> prev=nullptr;
if(head == nullptr)
{
head = temp;
tail = temp;
}
else
{
temp -> next = head;
head -> prev = temp;
head = temp;
}
count++;
}
template void LinkedList::AddTail(const T& data) {
Node* temp = new Node;
temp -> data = data;
temp -> next = nullptr;
temp -> prev = nullptr;
if(tail==nullptr)
{
head = temp;
}
else
{
tail -> next = temp;
temp -> prev = tail;
}
tail = temp;
count++;
}
template unsigned int LinkedList::NodeCount() const
{
return count;
}
template void LinkedList::PrintForward() const {
Node* temp = head;
while(temp != nullptr) {
cout
temp -> data
endl;
temp = temp -> next;
}
}
template void LinkedList::PrintReverse() const
{
Node* temp = tail;
while(temp != nullptr)
{
cout
temp -> data
endl;
temp = temp -> prev;
}
}
template void LinkedList::PrintForwardRecursive(const Node* node) const
{
if (node != nullptr)
{
cout
node -> data
endl;
PrintForwardRecursive(node -> next);
}
return;
}
template void LinkedList::PrintReverseRecursive(const Node* node) const
{
if(node != nullptr)
{
PrintReverseRecursive(node -> next);
cout
node -> data
endl;
}
return;
}
template void LinkedList::AddNodesHead(const T* data, unsigned int count)
{
for(int i = count-1; i >= 0; i--)
AddHead(data[i]);
}
template void LinkedList::AddNodesTail(const T* data, unsigned int count) {
for (unsigned int i = 0; i < count; i++) {
AddTail(data[i]);
}
}
template const typename LinkedList::Node* LinkedList::Head() const {
return head;
}
template typename LinkedList::Node* LinkedList::Head() {
return head;
}
template const typename LinkedList::Node* LinkedList::Tail() const {
return tail;
}
template typename LinkedList::Node* LinkedList::Tail() {
return tail;
}
template typename LinkedList::Node* LinkedList::GetNode(unsigned int index)
{
if (index < 0 || index >= count)
throw - 1;

Node* temp = head;
for (unsigned int i = 0; i < index; i++)
temp = temp -> next;
return temp;
}
template T &LinkedList::operator[](unsigned int index) {
Node* temp = GetNode(index);
return temp -> data;
}
template ool LinkedList::operator==(const LinkedList &rhs) const {
if (count != rhs.count) {
return false;
}
Node* List1Iterator =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here