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. You add a dish on top, and that 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
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; // a simple class to store in the array public class Tester { static void Main( ) { Stack intStack = new Stack( ); // populate the arrayfor (int i = 0;i<8;i++)
{
intStack.Push(i*5);
}
// Display the Queue. Console.Write( "intStack values: " ); PrintValues( intStack ); // Remove an element from the queue. Console.WriteLine( " (Pop) {0}",intStack.Pop( ) );
// Display the Queue. Console.Write( "intStack values: " ); PrintValues( intStack ); // Remove another element from the queue. Console.WriteLine( " (Pop) {0}", intStack.Pop( ) ); // Display the Queue. Console.Write( "intStack values: " ); PrintValues( intStack ); // View the first element in the // Queue but do not remove. Console.WriteLine( " (Peek) {0}",intStack.Peek( ) );
// Display the Queue. Console.Write( "intStack values: " ); 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( " Target 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( " Target 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( " The 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 25
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 = myIntStack.ToArray( );
3.133.142.2