C++ Stack Using Templated Linked List - Memory Leaks -
I'm trying to figure out how to remove all memory leaks from this code. I know that this problem may be by using the push (t-value)
function new node & lt; T & gt; (Value)
, but I do not know how to fix it. / P>
#ifndef MYSTACK_H #define MYSTACK_H #include & lt; Stdexcept & gt; using namespace std; Template & lt; Class T & gt; Class Mistakes {template & lt; Class G & gt; Square node {public: g; value; Node * next; Node (t wall, node * n = null): value (wal), next (n) {}; }; Public: MyStacks () {Head = Null; Tail = null; My_size = 0; }; ~ Mistakes () {Removing Head; } Zero Push (T-value) {If (head == faucet) {head = new node & lt; T & gt; (value); Tail = head; My_size ++; } And {node & lt; T & gt; * Temp = new node & lt; T & gt; (value); Temp- & gt; Next = head; Head = temporary; My_size ++; }} // . (Deleted - Summary) Private: Unsigned int my_size; The node's & lt; T & gt; * Head; The node's & lt; T & gt; * Tails; }; #endif
Any help would be appreciated! Thanks!
You have to delete all the elements in your stack. Here are some ways to do this:
- In your destructor, loop through all the elements of your stack and delete them.
- In your destructor, call repeatedly
pop
until the stack is empty.
Option 2 is probably easy for code, assuming that you already have a pop
function, but possibly less efficient because this stack To make sure the structure will be maintained, the structure is maintained, while it is being destroyed.
Comments
Post a Comment