// Fig. 26.5: Stack.cs // Generic class Stack using System; class Stack< E > { private int top; // location of the top element private E[] elements; // array that stores Stack elements // parameterless constructor creates a Stack of the default size public Stack() : this( 10 ) // default stack size { // empty constructor; calls constructor at line 18 to perform init } // end Stack constructor // constructor creates a Stack of the specified number of elements public Stack( int stackSize ) { if ( stackSize > 0 ) // validate stackSize elements = new E[ stackSize ]; // create stackSize elements else elements = new E[ 10 ]; // create 10 elements top = -1; // Stack initially empty } // end Stack constructor // push element onto the Stack; if successful, return true // otherwise, throw FullStackException public void Push( E pushValue ) { if ( top == elements.Length - 1 ) // Stack is full throw new FullStackException( String.Format( "Stack is full, cannot push {0}", pushValue ) ); top++; // increment top elements[ top ] = pushValue; // place pushValue on Stack } // end method Push // return the top element if not empty // else throw EmptyStackException public E Pop() { if ( top == -1 ) // Stack is empty throw new EmptyStackException( "Stack is empty, cannot pop" ); top--; // decrement top return elements[ top + 1 ]; // return top value } // emd method Pop } // end class Stack /************************************************************************** * (C) Copyright 1992-2006 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/