title: C++ Stack Implementation
body: <p>Hey all! Having a little trouble with my stack. Im trying to print each element that I've pushed onto the stack.</p>
  
  <p>Starting with the stack ctor we know that we have a fixed size for the array. So I allocate the items struct object to hold just that much space:</p>
  
  <pre><code>stack::stack(int capacity)
  {   
    items = new item[capacity];
  
    if ( items == NULL ) {
      throw "Cannot Allocoate Sufficient Memmory";
      exit(1); 
    }
    maxSize = capacity;
    top       = -1;
  }
  </code></pre>
  
  <p>Yes, items is a struct type of the object "item". Have a look:</p>
  
  <pre><code>class stack
  {
    stack(int capacity);
    ~stack(void);
  ...
    private:
      int maxSize; // is for the item stack
      int top;     // is the top of the stack
      struct item {
        int n;
      };
      item        *items;                 
  
    public:
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, stack&amp; q)
    ...
  </code></pre>
  
  <p>First and formost we want to add to the stack by pushing each incoming element into the array FILO:</p>
  
  <pre><code>bool stack::pushFront( const int n )
  {
       if ( top &gt;= maxSize-1 ) 
  {
      throw "Stack Full On Push";
      return false;
  }
  else 
  {
      ++top;
      items[top].n = n;
  }
  return true;
  }
  
  // just a textbook example here:
  stack::~stack(void)
  {
    delete [] items;
  
    items    = NULL;
    maxSize = 0;
    top      = -1;
  }
  </code></pre>
  
  <p>Yes the real issue for me is the items[++top].n = n; statement. I've been trying to find out how I can drag (+) the items array out to see ALL of the array elements after I push onto the stack.</p>
  
  <p>Im wondering why I cant drag that items[++top].n = n statement out when im debugging. All that comes up is the value that is passed as an 'n' paramater. Do I need to use a stack object type array to store the values into?</p>
  
  <p>When I overload the &lt;&lt; operator and try to print the elements I get an insanely large negative number:</p>
  
  <pre><code>ostream&amp; operator&lt;&lt;(ostream&amp; out, stack&amp; q)
  {
      if ( q.top &lt;= 0 ) // bad check for empty or full node
      out &lt;&lt; endl &lt;&lt; "stack: empty" &lt;&lt; endl &lt;&lt; endl;
      else
          for ( int x = 0; x &lt; q.maxSize; x++ )
          {
              out &lt;&lt; q.items[x].n; // try to print elements
          }
      return out;
  }
  </code></pre>
  
  <p>I'm way off and I need some guidence if anyone has the time!</p>