I have the following c++ data structures assignment: Copy Constructors, Destruct
ID: 665119 • Letter: I
Question
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 them, you must implement the deepCopy() and makeEmpty() methods. Please see the code below.
namespace cs20a {
template<class T>
List<T>::List()
{
head = tail = &header;
header.next = NULL;
size = 0;
}
template<class T>
List<T>::List(const List &rhs)
{
deepCopy(rhs);
}
template <Class T>
List<T>::~List()
{
makeEmpty();
}
template<class T>
List<T> & List<T>::operator = (const List &rhs)
{
if (this == &rhs)
return *this;
makeEmpty();
deepCopy(rhs);
return *this;
}
template<class T>
void List<T>::makeEmpty()
{ // *** Write this code *** }
template<class T>
inline void List<T>::deepCopy(const List &rhs)
{ // *** Write this code *** }
Please submit all of the classes and headers along with your test driver.
//This is the code for the working list iterator.
List.h
#pragma once
#include "ListNode.h"
#include "ListIterator.h"
namespace cs20
{
template<class T> class ListIterator;
template <class T>
class List
{
public:
typedef ListIterator<T> iterator;
typedef ListIterator<T> const_iterator;
List();
List(const List &original);
~List();
List<T> & operator = (const List<T> &original);
void append(const T &t);
bool remove(const T &t);
void clear();
bool contains(const T &t);
bool isEmpty() const;
int getSize() const;
iterator begin()
{ return iterator(head->next); }
const const_iterator cbegin() const
{ return const_iterator(head->next); }
iterator end()
{ return iterator(nullptr); }
const const_iterator cend() const
{ return const_iterator(nullptr); }
iterator back()
{ return iterator(tail==head ? tail->next : tail); }
const const_iterator cback() const
{ return const_iterator(tail == head ? tail->next : tail); }
private:
ListNode<T> *head;
ListNode<T> *tail;
ListNode<T> header;
int size;
inline void deepCopy(const List<T> &original);
inline void makeEmpty();
};
}
List.cpp
#pragma once
#include <iostream>
#include "List.h"
#include "ListNode.h"
namespace cs20
{
template<class T>
List<T>::List()
{
head = tail = &header;
header.next = NULL;
size = 0;
}
template<class T>
List<T>::List(const List &rhs)
{
deepCopy(rhs);
}
template<class T>
List<T>::~List()
{
makeEmpty();
}
template<class T>
List<T> & List<T>::operator = (const List<T> &rhs)
{
if (this == &rhs)
return *this;
makeEmpty();
deepCopy(rhs);
return *this;
}
template<class T>
void List<T>::makeEmpty()
{
// *** Write this code ***
}
template<class T>
inline void List<T>::deepCopy(const List<T> &rhs)
{
// *** Write this code ***
}
template<class T>
bool List<T>::isEmpty() const
{
return size == 0;
}
template<class T>
void List<T>::append(const T &t)
{
ListNode<T> *node = new ListNode<T>(t);
node->previous = tail;
tail->next = node;
node->next = NULL;
tail = node;
size++;
}
template<class T>
bool List<T>::remove(const T &t)
{
if (head->next == NULL)
return false;
ListNode<T> *current = head;
while (current->next != NULL)
{
if (current->next->value == t)
{
ListNode<T> *node = current->next;
current->next = node->next;
if (node->next != NULL)
node->next->previous = current;
delete node;
size--;
return true;
}
current = current->next;
}
return false;
}
template<class T>
void List<T>::clear()
{
makeEmpty();
head = tail = &header;
header.next = NULL;
size = 0;
}
template<class T>
bool List<T>::contains(const T &t)
{
if (head->next == NULL)
return false;
ListNode<T> *current = head->next;
while (current != NULL)
{
if (current->value == t)
return true;
current = current->next;
}
return false;
}
template<class T>
int List<T>::getSize() const
{
return size;
}
}
ListIterator.h
#pragma once
#include "List.h"
#include "ListNode.h"
namespace cs20
{
template <class T>
class ListIterator
{
public:
ListIterator();
ListIterator(ListNode<T>* ptr);
ListIterator<T> & operator ++ ( );
ListIterator<T> operator ++ (int);
bool operator == (const ListIterator<T>& li) const;
bool operator != (const ListIterator<T>& li) const;
T & getValue() const;
T & operator * () const;
T & operator -> () const;
operator ListIterator<const T>() const;
typedef ListIterator<T> iterator;
private:
ListNode<T>* m_ptr;
};
}
ListIterator.cpp
#pragma once
#include <iostream>
#include "ListNode.h"
#include "ListIterator.h"
// http://codereview.stackexchange.com/questions/74609/custom-iterator-for-a-linked-list-class
namespace cs20
{
template<class T>
ListIterator<T>::ListIterator() : m_ptr(NULL)
{}
template<class T>
ListIterator<T>::ListIterator(ListNode<T>* ptr) : m_ptr(ptr)
{}
template<class T>
ListIterator<T> & ListIterator<T>::operator ++ () // Pre-increment
{
m_ptr = m_ptr->next;
return *this;
}
template<class T>
ListIterator<T> ListIterator<T>::operator ++ (int) // Post-increment
{
ListIterator<T> tmp(*this);
m_ptr = m_ptr->next;
return tmp;
}
template<class T>
bool ListIterator<T>::operator == (const ListIterator<T>& li) const
{
return m_ptr==li.m_ptr;
}
template<class T>
bool ListIterator<T>::operator != (const ListIterator<T>& li) const
{
return m_ptr!=li.m_ptr;
}
template<class T>
T & ListIterator<T>::operator* () const
{
return m_ptr->value;
}
template<class T>
T & ListIterator<T>::operator-> () const
{
return m_ptr->value;
}
// One way conversion: iterator -> const_iterator
template<class T>
ListIterator<T>::operator ListIterator<const T>() const
{
return ListIterator<const T>(m_ptr);
}
}
ListNode.h
#pragma once
namespace cs20
{
template <class T>
struct ListNode
{
ListNode()
{};
ListNode(T t): value(t), next(nullptr), previous(nullptr)
{}
T value;
ListNode *next;
ListNode *previous;
};
}
//this is what I have so far, thank you for the help in advance.
Explanation / Answer
}
template <class T> void list<T>::makeEmpty() { node<T>* temp; while ( listData != NULL ) { temp = listData; listData = listData->next; delete temp; } length = 0;}