// Fig 26.9: StackTest.cs // Stack generic class test program. using System; using System.Collections.Generic; class StackTest { // create arrays of doubles and ints static double[] doubleElements = new double[]{ 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 }; static int[] intElements = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; static Stack< double >doubleStack; // stack stores double objects static Stack< int >intStack; // stack stores int objects static void Main( string[] args ) { doubleStack = new Stack< double >( 5 ); // Stack of doubles intStack = new Stack< int >( 10 ); // Stack of ints // push doubles onto doubleStack TestPush( "doubleStack", doubleStack, doubleElements ); // pop doubles from doubleStack TestPop( "doubleStack", doubleStack ); // push ints onto intStack TestPush( "intStack", intStack, intElements ); // pop ints from intStack TestPop( "intStack", intStack ); } // end Main static void TestPush< E >( string name, Stack< E > stack, IEnumerable< E > elements ) { // push elements onto stack try { Console.WriteLine( "\nPushing elements onto " + name ); // push elements onto stack foreach ( E element in elements ) { Console.Write( "{0} ", element ); stack.Push( element ); // push onto stack } // end foreach } // end try catch ( FullStackException exception ) { Console.Error.WriteLine(); Console.Error.WriteLine( "Message: " + exception.Message ); Console.Error.WriteLine( exception.StackTrace ); } // end catch } // end method TestPush static void TestPop< E >( string name, Stack< E > stack ) { // push elements onto stack try { Console.WriteLine( "\nPopping elements from " + name ); E popValue; // store element removed from stack // remove all elements from Stack while ( true ) { popValue = stack.Pop(); // pop from stack Console.Write( "{0} ", popValue ); } // end while } // end try catch ( EmptyStackException exception ) { Console.Error.WriteLine(); Console.Error.WriteLine( "Message: " + exception.Message ); Console.Error.WriteLine( exception.StackTrace ); } // end catch } // end TestPop } // end class StackTest /************************************************************************** * (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. * *************************************************************************/