Section 9.8. Stacks


9.8. Stacks

A stack is a last-in, first-out (LIFO) collection, like a stack of dishes at a buffet table, or a stack of coins on your desk. A dish added on top is the first dish you take off the stack.

The principal methods for adding to and removing from a stack are Push() and Pop(); Stack also offers a Peek() method, very much like Queue. The significant methods and properties for Stack are shown in Table 9-5.

Table 9-5. Stack methods and properties

Method or property

Purpose

Count

Public property that gets the number of elements in the Stack.

Clear( )

Removes all objects from the Stack.

Clone( )

Creates a shallow copy.

Contains( )

Determines if an element is in the Stack.

CopyTo( )

Copies the Stack elements to an existing one-dimensional array.

GetEnumerator( )

Returns an enumerator for the Stack.

Peek( )

Returns the object at the top of the Stack without removing it.

Pop( )

Removes and returns the object at the top of the Stack.

Push( )

Inserts an object at the top of the Stack.

ToArray( )

Copies the elements to a new array.


The List, Queue, and Stack types contain overloaded CopyTo( ) and ToArray( ) methods for copying their elements to an array. In the case of a Stack, the CopyTo( ) method will copy its elements to an existing one-dimensional array, overwriting the contents of the array beginning at the index you specify. The ToArray( ) method returns a new array with the contents of the stack's elements. Example 9-17 illustrates.

Example 9-17. Working with a stack
#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace Stack {    public class Tester    {       static void Main( )       {          Stack<Int32> intStack = new Stack<Int32>( );          // populate the array          for ( int i = 0; i < 8; i++ )          {             intStack.Push( i * 5 );          }          // Display the Stack.          Console.Write( "intStack values:\t" );          PrintValues( intStack );          // Remove an element from the stack.          Console.WriteLine( "\n(Pop)\t{0}",          intStack.Pop( ) );          // Display the Stack.          Console.Write( "intStack values:\t" );          PrintValues( intStack );          // Remove another element from the stack.          Console.WriteLine( "\n(Pop)\t{0}",             intStack.Pop( ) );          // Display the Stack.          Console.Write( "intStack values:\t" );          PrintValues( intStack );          // View the first element in the           // Stack but do not remove.          Console.WriteLine( "\n(Peek)   \t{0}",             intStack.Peek( ) );          // Display the Stack.          Console.Write( "intStack values:\t" );          PrintValues( intStack );          // declare an array object which will           // hold 12 integers          int[] targetArray = new int[12];          for (int i = 0; i < targetArray.Length; i++)          {              targetArray[i] = i * 100 + 100;          }         // Display the values of the target Array instance.         Console.WriteLine( "\nTarget array:  " );         PrintValues( targetArray );          // Copy the entire source Stack to the            // target Array instance, starting at index 6.          intStack.CopyTo( targetArray, 6 );          // Display the values of the target Array instance.          Console.WriteLine( "\nTarget array after copy:  " );          PrintValues( targetArray );       }       public static void PrintValues(          IEnumerable<Int32> myCollection )       {          IEnumerator<Int32> enumerator =              myCollection.GetEnumerator( );          while ( enumerator.MoveNext( ) )             Console.Write( "{0}  ", enumerator.Current );          Console.WriteLine( );       }    } } Output: intStack values:        35  30  25  20  15  10  5  0 (Pop)   35 intStack values:        30  25  20  15  10  5  0 (Pop)   30 intStack values:        25  20  15  10  5  0 (Peek)          25 intStack values:        25  20  15  10  5  0 Target array: 100  200  300  400  500  600  700  800  900  0  0  0 Target array after copy: 100  200  300  400  500  600  25  20  15  10  5  0 The new  array: 25  20  15  10  5  0

The output reflects that the items pushed onto the stack were popped in reverse order.

The effect of CopyTo() can be seen by examining the target array before and after calling CopyTo( ). The array elements are overwritten beginning with the index specified (6).



Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net