Question

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 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()
   {
node<T>* temp;
       while ( listData != NULL )
       {
           temp = listData;
           listData = listData->next;
           delete temp;
       }
           length = 0;
   }

   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. What I need help with is how to implement the deepCopy method properly, where there is the comment "write this code". I also need a driver to run this program.

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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...

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

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

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

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