Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy Constructor of Stack using Linked Lists
    primarykey
    data
    text
    <p>First making a copy constructor for linked lists was difficult and now this stack. I almost wanted to hit my head with something but then I thought of Stack Overflow guys. So here is the problem: Consider One thing You can not change the list.h or stack.h or its constructor even. // In list.h</p> <pre><code>template &lt;class T&gt; struct ListItem { T value; ListItem&lt;T&gt; *next; ListItem&lt;T&gt; *prev; ListItem(T theVal) { this-&gt;value = theVal; this-&gt;next = NULL; this-&gt;prev = NULL; } }; /* This is the generic List class */ template &lt;class T&gt; class List { ListItem&lt;T&gt; *head; public: // Constructor List(); // Copy Constructor List(const List&lt;T&gt;&amp; otherList); } // In list.cpp template &lt;class T&gt; List&lt;T&gt;::List() { head=NULL; } template &lt;class T&gt; List&lt;T&gt;::List(const List&lt;T&gt;&amp; otherList) { // I have code working for this part } template &lt;class T&gt; List&lt;T&gt;::~List() { } // In stack.h (includes list.cpp) template &lt;class T&gt; class Stack { List&lt;T&gt; list; public: Stack(); Stack(const Stack&lt;T&gt;&amp; otherStack); ~Stack(); void push(T item); T top(); T pop(); }; // remember top(); pop(); push() functions are working properly in stack.cpp file. // In stack.cpp (includes stack.h) Stack(const Stack&lt;T&gt;&amp; otherStack){ } template &lt;class T&gt; void Stack&lt;T&gt;::push(T item) { } template &lt;class T&gt; T Stack&lt;T&gt;::top() { } template &lt;class T&gt; T Stack&lt;T&gt;::pop() { } </code></pre> <p>there is an object s containing elem from 0 to 100. 100 being on the top. Now we copy something like this:-</p> <pre><code>Stack&lt;int&gt; s2(s); </code></pre> <p>I don't know how on Earth to access otherStack elements. I mean of course its a linked list. But it is in Stack.cpp, what can I do to access it and also how to make a copy constructor for this stack (Working code would be preferable). And Please be supportive this time. Thanks. NOTE: You can't change any constructor. It has to be the way it is. I hope every body this time gets my query.</p>
    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