|
using
System;
using
System.Collections;
class
StackDemo
{
public
static void Main()
{
//Create an instance of Stack
Stack S = new Stack();
//Add elements to the Stack using Push() Method
S.Push("Prasad");
S.Push("Giri");
S.Push("Ravi");
//Pop() removes and returns the object at the top of the Stack
Console.WriteLine(S.Pop());
//Count determines the number of elements contained in the Stack
//Count will be 2 as we removed the top object from the Stack
Console.WriteLine("Count = " + S.Count);
//Peek() method returns the object at the top of the Stack without
//removing from the Stack
Console.WriteLine(S.Peek());
//The count will still be 2 as the peek() method will not remove the object
//from the Stack
Console.WriteLine("Count = " + S.Count);
//Remove all the items from the Stack using Clear() method
S.Clear();
}
}
|