| |
Stack demo |
|
|
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
using System.Collections;
namespace StackDemo
{
public class TesterStackDemo
{
public void Run()
{
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" );
DisplayValues( intStack );
// Remove an element from the stack.
Console.WriteLine( "\n(Pop)\t{0}",
intStack.Pop() );
// Display the Stack.
Console.Write( "intStack values:\t" );
DisplayValues( intStack );
// Remove another element from the stack.
Console.WriteLine( "\n(Pop)\t{0}",
intStack.Pop() );
// Display the Stack.
Console.Write( "intStack values:\t" );
DisplayValues( 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" );
DisplayValues( intStack );
}
public static void DisplayValues(
IEnumerable myCollection )
{
foreach (object o in myCollection)
{
Console.WriteLine(o);
}
}
[STAThread]
static void Main()
{
TesterStackDemo t = new TesterStackDemo();
t.Run();
}
}
}
|
|
|
Related examples in the same category |
|