Examples of Stack
Examples of Stack
Name Description
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.
The following example shows how to create and add values to a Stack and how to display its
values.
C#
C++
VB
using System;
using System.Collections;
public class SamplesStack {
myStack
Count: 3
Values: ! World Hello*/
The following example shows how to add elements to the Stack, remove elements from
the Stack, or view the element at the top of the Stack.
C#
C++
VB
using System;
using System.Collections;
public class SamplesStack {
// Views the first element in the Stack but does not remove it.
Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );
/*
This code produces the following output.
The following example shows how to clear the values of the Stack.
C#
C++
VB
using System;
using System.Collections;
}
public static void PrintValues( IEnumerable myCollection ) {
foreach ( Object obj in myCollection )
Console.Write( " {0}", obj );
Console.WriteLine();
}
/*
This code produces the following output.
Initially,
Count : 5
Values: jumped fox brown quick The
After Clear,
Count : 0
Values:
*/