9.7 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

Synchronized( )

Public static method that returns a thread-safe Stack wrapper.

Count

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

IsSynchronized

Public property that gets a value indicating if the Stack is synchronized.

SyncRoot

Public property that returns an object that can be used to synchronize access to 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 ArrayList, 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-16 illustrates.

Example 9-16. Working with a Stack
namespace Programming_CSharp {    using System;    using System.Collections;       public class Tester    {       static void Main( )       {          Stack intStack = new Stack( );          // 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          Array targetArray=Array.CreateInstance(              typeof(int), 12  );          targetArray.SetValue( 100, 0 );          targetArray.SetValue( 200, 1 );          targetArray.SetValue( 300, 2 );          targetArray.SetValue( 400, 3 );          targetArray.SetValue( 500, 4 );          targetArray.SetValue( 600, 5 );          targetArray.SetValue( 700, 6 );          targetArray.SetValue( 800, 7 );          targetArray.SetValue( 900, 8 );          // 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 );          // Copy the entire source Stack           // to a new standard array.                   Object[] myArray = intStack.ToArray( );          // Display the values of the new standard array.          Console.WriteLine( "\nThe new  array:" );          PrintValues( myArray );       }       public static void PrintValues(           IEnumerable myCollection )         {          System.Collections.IEnumerator 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. In fact, the entire stack is stored in reverse order to reflect its LIFO nature.

Example 9-16 uses the Array class that serves as the base class for all arrays. The example creates an array of 12 integers by calling the static method of CreateInstance( ). This method takes two arguments: a type (in this case, int) and a number representing the size of the array.

The array is populated with the SetValue( ) method which takes two arguments: the object to add and the offset at which to add it.

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).

Notice also that the ToArray( ) method is designed to return an array of objects, and so myArray is declared appropriately:

Object[] myArray = intStack.ToArray( );


Programming C#
C# Programming: From Problem Analysis to Program Design
ISBN: 1423901460
EAN: 2147483647
Year: 2003
Pages: 182
Authors: Barbara Doyle

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