Q:

C# program to check whether element exists in stack or not

0

C# program to check whether element exists in stack or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Data Structure Solved Programs/Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to copy stack elements to array... >>
<< C# program to pop elements from stack using collec...