Note that there are some explanatory texts on larger screens.

plurals
  1. PONode-based stack class (need peer review)
    primarykey
    data
    text
    <p>I recently wrote a node-based stack class, per instructions (specs in the comments before the code, taken from the forum post). I was told to post it here for review by one of the friendlier members of the SO community, so here it is. For simplicity's sake: I put the definitions with the implementation. I understand when to use header files =) </p> <p>Mainly, I want to know if my use of delete is sound. I am still unsure of myself when it comes to using destructors; the specs made it sound like the ONLY time I should be deleting nodes should be during pop, and anything else is unsafe. I also don't understand the use of a copy constructor/assignment constructor here.</p> <p>Anyways, any bugs or comments about the code would be great. </p> <pre><code>/*stack class Background: the specs for this are, verbatim: "Write a node-based stack class smile.gif The stack is one of the most fundamental data structures used in computer science. A stack has three basic operations: push(value) - puts a value on the top of the stack pop() - removes and returns the value that's on the top of the stack peek() - return (but does not remove) the value off the top of the stack Before creating the stack, you first have to create a Node class, which is a very basic class with just two member variables: the value of the node, and a pointer to the previous node in the stack. Your stack should have only one member variable: the top node of the stack. When you push, you add a node with the new value, with it's previous pointer pointing towards the current stack top item. When you pop, you delete the top node and then set the top of the stack to whatever that node's previous node pointer was. push, pop, and peek must all run in constant time. You should write it so that it can only push (and pop/peek) ints." */ #include &lt;string&gt; #include &lt;iostream&gt; class Node { private: int value; Node* prev; public: int returnValue() { return value; } Node* returnPtr() { return prev; } /* constructors and destructors */ Node(int val, Node* ptrToLast) { value = val; prev = ptrToLast; } }; class Stack { private: Node* top; int size; public: Stack() { size = 0; top = NULL; } //added this after told the need for a destructor; not sure if it works ~Stack() { while (top != NULL) { Node* tempPtr = top.returnPtr(); delete top; top = tempPtr; } } Node* returnTopPtr() { return top; } void push(int); int pop(); int peek(); //bonus; figured it might be worth knowing how many //nodes are in a given stack int returnSize(); }; int Stack::returnSize() { return size; } void Stack::push(int value) { ++size; Node* tempPtr = top; top = new Node(value, tempPtr); } int Stack::peek() { return top-&gt;returnValue(); } int Stack::pop() { const std::string throwStr = "You are trying to access/delete a node that doesn't exist. Seriously. "; if (size == 0) { throw(throwStr); } --size; Node* tempPtr = top-&gt;returnPtr(); int tempVal = top-&gt;returnValue(); delete top; top = tempPtr; return tempVal; } </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.
 

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