// Stack.C // // implements the Stack class described // in the lecture notes for CS341 Spring 1998 #include #include "Stack.h" // default constructor //template //Stack::Stack( ) //{ // Stack_Array = new Element_Type [Default_Size]; // Top_Of_Stack = -1; // Full_Stack = Default_Size; /// // cout << "created a default stack" << endl; //} // construct Stack with N elements template Stack::Stack(unsigned int N) { Stack_Array = new Element_Type [N]; Top_Of_Stack = -1; Full_Stack = N; cout << "created a stack with size " << N << endl; } // Pop() // "remove" the element at the // the top of the stack by just // decrementing the index template void Stack::Pop() { if (! isEmpty()) --Top_Of_Stack; else cout << "Tried to Pop() empty stack" << endl; } // Push () // insert a data element on the // top of the stack template void Stack::Push (const Element_Type& X) { if (! isFull()) Stack_Array[++Top_Of_Stack] = X; else cout << "Tried to Push() onto full stack" << endl; } // Pop_And_Top // remove and return the top element of the stack template Element_Type Stack::Pop_And_Top () { if (! isEmpty()) return Stack_Array[Top_Of_Stack--]; // else what if stack is empty?? return Element_Type(0); } // Top() // return const reference to the element // on the top of the stack without removing it template const Element_Type& Stack::Top() const { if (! isEmpty()) return Stack_Array[Top_Of_Stack]; // else -- what if stack is empty?? return Element_Type(0); }