Program to check given element exists in stack or not in C#
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Stack S = new Stack(5);
S.Push(10);
S.Push(20);
S.Push(30);
S.Push(40);
if (S.Contains(30))
Console.WriteLine("Item found in stack");
else
Console.WriteLine("Item did not find in stack");
}
}
}
Output
Item found in stack
In this program, we are pushing 4 elements (10,20,30,40) in stack, and then we are checking item 30 is exist in stack or not using Contains method of Stack class?
Note: In above program, to use 'Stack' class, we need to include System.Collection namespace.
Program to check given element exists in stack or not in C#
Output
In this program, we are pushing 4 elements (10,20,30,40) in stack, and then we are checking item 30 is exist in stack or not using Contains method of Stack class?
Note: In above program, to use 'Stack' class, we need to include System.Collection namespace.