Stack problems

I've written my own stack class as a homework assignment in my computer science class. I've run into a problem that I absolutely can no seem to figure out inside this stack class. it appears the stack is somehow either modifying the values I store in it or not storing them properly and giving me these wild answers. In the code I'm posting below the couts are for my purpose in tracking what my functions are actually doing. oddly enough I don't get the outputs these couts are sending to the console when the functions are called from main, or anywhere else. If I could see the values i'm sending to the console I believe I can fix this problem but I don't know why my couts aren't working. I'm definitely getting into the functions in main as the value of size increases and decreases. please help me figure this one out I've been starring at this for hours to no avail.


////////////////////////////////////////////////////////////////////////////////
#include<iostream>
using namespace std;
#ifndef STACK_H
#define STACK_H
////////////////////////////////////////////////////////////////////////////////
//create template for generic reusablility
template <class Element>
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//begin class
class Stack
{
private:
struct node
{
node* previous;
Element x;
};
node* top;
int size;
public:
Stack();
Stack(Element);
void push(Element);
Element pop();
Element peek();
bool empty();
int getSize();
};
////////////////////////////////////////////////////////////////////////////////
#endif


////////////////////////////////////////////////////////////////////////////////
//constructors

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//Default //
//initializes an empty stack //
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class Element>
Stack<Element>::Stack()
{
top = 0;
size=0;
cout<<"init"<<endl;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//Stack(Element init) //
//initialize the stack with a first element //
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class Element>
Stack<Element>::Stack(Element init)
{
//create first node
node newNode;
newNode.x=init;
newNode.previous=0;

//set top
top = &newNode;

//set size
size=1;
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//functions

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//push //
//Places an item on the top of the stack //
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class Element>
void Stack<Element>::push(Element in)
{
cout<<"{"<<endl<<top<<endl;
//create new node
node newNode;
newNode.x=in;
newNode.previous=top;

//add node to list
top = &newNode;
cout<<&newNode<<endl<<top<<endl;
//increase size
size++;
cout<<"What is going on argh"<<endl;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//pop //
//removes and returns the top item of the stack //
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class Element>
Element Stack<Element>::pop()
{
//retrieve top element
Element holder = top->x;

//delete node
node* prev=top->previous;
delete top;
top=prev;

//decrease size
size--;

//return element
return holder;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//peek //
//returns the top element on the stack without removing it //
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class Element>
Element Stack<Element>::peek()
{
return top->x;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//empty //
//returns true if there is nothing in the stack //
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template<class Element>
bool Stack<Element>::empty()
{
return size==0;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//getSize //
//returns the size of the stack //
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template<class Element>
int Stack<Element>::getSize()
{
return size;
}
Topic archived. No new replies allowed.