Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are many style issues that warrant discussion - but the biggest issue is that whenever you explicitly manage dynamic memory within your class, at a minimum you need a user defined destructor, a copy constructor and an assignment operator to handle all the dynamic memory issues appropriately - or else you will have memory leaks and undefined deletions. The reason you need to explicitly define these functions is because you need a copy or an assignment to make copies of the structures that your head pointer and subsequent nodes point to and not just copy the address that they point to (which is the default behavior of the implementations provided by the compiler) - and the default destructor does not ever delete dynamically allocated memory - which is why you need to define a destructor that does.</p> <p>Here is a reasonable implementation that you can play with - but more importantly, contrast it with an approach (included at the end) that uses vector and does not have to deal at all with explicit memory management:</p> <pre> class Stack { // a nested implementation class that the client needs to know nothing about struct Node { Node* prev; int value; Node(Node* prev, int value) : prev(prev), value(value) { } Node() : prev(0), value(0) { } ~Node() { delete prev; } // clean up after yourself // copy recursively until you hit a null pointer Node(const Node& o) : value(o.value), prev( prev ? new Node(*prev) : 0 ) { } }; Node* head_; int size_; public: Stack() : head_(0), size_(0) { } ~Stack() { delete head_; } // copy recursively until null Stack(const Stack& o) : head_(o.head_ ? new Node(*o.head_) : 0) { } // use copy constructor to do assignment Stack& operator=(const Stack& o) { Stack copy(o); Node* cur = head_; head_ = copy.head_; size_ = copy.size_; copy.head_ = cur; // copy's destructor will delete return *this; } void push(int value) { head_ = new Node(head_,value); ++size_; } int peek() const { if (!head_) throw "Attempting to peek off an empty stack!"; return head_->value; } int pop() { if (!head_) throw "Attempting to pop off an empty stack!"; int ret = head_->value; Node* cur = head_; // hold on to it so we can delete it head_ = head_->prev; // adjust my pointer cur->prev = 0; // if this is not set to 0, all nodes will be deleted delete cur; --size_; return ret; } int size() const { return size_; } }; // -- an easier way to write a stack of ints ;) struct VecStack { std::vector&lt;int&gt; vec; void push(int x) { vec.push_back(x); } int peek() const { if(vec.empty()) throw "Is Empty"; return *--vec.end(); // you may prefer vec[vec.size() - 1]; } int pop() { if (vec.empty()) throw "Is Empty"; int ret = *--vec.end(); vec.pop_back(); return ret; } int size() const { return vec.size(); } }; </pre>
 

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