Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this a deep copy and properly implemented = operator for a C++ Linked List?
    primarykey
    data
    text
    <p>I'm running into some bugs now that I'm trying to use the doubly linked list class that I made. My implementation of the = operator looks like the following:</p> <pre><code>template &lt;typename T&gt; Dlist&lt;T&gt;&amp; Dlist&lt;T&gt;::operator=(const Dlist &amp;l) { copyAll(l); return *this; } template &lt;typename T&gt; void Dlist&lt;T&gt;::copyAll(const Dlist &amp;l) { node *copyList = new node; copyList = l.first; while(copyList){ insertFront(copyList.first-&gt;o); copyList = copyList-&gt;next; } delete copyList; } </code></pre> <p>Note that o is a pointer for the data in a node in the List.</p> <p>My intent is for copyAll to be a true deep copy. Is this not the case? Is there something wrong with my class method definitions here? I'm new to linked lists so help is much appreciated!</p> <p>EDIT: specifically the issue I'm having is when I make a List and fill it, then make a new list and set it equal to the first, anytime I do something to the second list, it also alters the first list. </p> <p>EDIT2: Here's the class itself. I'm not allowed to add any other member functions:</p> <pre><code>template &lt;typename T&gt; class Dlist { public: // Operational methods bool isEmpty(); // EFFECTS: returns true if list is empty, false otherwise void insertFront(T *o); // MODIFIES this // EFFECTS inserts o at the front of the list void insertBack(T *o); // MODIFIES this // EFFECTS inserts o at the back of the list T *removeFront(); // MODIFIES this // EFFECTS removes and returns first object from non-empty list // throws an instance of emptyList if empty T *removeBack(); // MODIFIES this // EFFECTS removes and returns last object from non-empty list // throws an instance of emptyList if empty // Maintenance methods Dlist(); // ctor Dlist(const Dlist &amp;l); // copy ctor Dlist &amp;operator=(const Dlist &amp;l); // assignment ~Dlist(); // dtor private: // A private type struct node { node *next; node *prev; T *o; }; node *first; // The pointer to the 1st node (NULL if none) node *last; // The pointer to the 2nd node (NULL if none) void makeEmpty(); // EFFECT: called by constructors/operator= to establish empty // list invariant void removeAll(); // EFFECT: called by destructor/operator= to remove and destroy // all list elements void copyAll(const Dlist &amp;l); // EFFECT: called by copy constructor/operator= to copy elements // from a source instance l to this instance }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload