Stack Class Working
The following code shows you the basic functionality of Stack class, for this you have to include namespace System.Collections.
Code
using System.Collections;
public class StackWorking
{
public static void Main(string[] args)
{
Stack st = new Stack();
// Pushing elements in Stack
st.Push("Osama");
st.Push("Mursleen");
st.Push("Maverick");
st.Push("Mascot");
st.Push("Veracious");
// Removing the very top element in stack
st.Pop();
Console.WriteLine("Stack Demonstration : ");
//Displaying Stack elements through foreach loop
foreach (Object o in st)
Console.WriteLine(o.ToString());
//Displaying Stack elements through for loop
for (int i = 1; i <= st.Count; i++)
Console.WriteLine(st.Pop().ToString());
//Stack Top Element
Console.WriteLine(st.Peek());
//Counts the total elements in stack
Console.WriteLine(st.Count);
//Converting Stack to an object array and displaying that new array
object[] stackArray = st.ToArray();
foreach (Object o in stackArray)
Console.WriteLine(o.ToString());
//Check whether the mentioned object is in stack or not.
if (st.Contains("Osama"))
{
Console.WriteLine("Yes It Is In Stack");
}
}
}
0 comments:
Post a Comment