Q:

example of LIFO approach using c# Programming

0

Write code that implements LIFO approach using c# programming

All Answers

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


using System;
using System.Collections.Generic;

class GFG
{
	// Pushing element on the top of the stack
	static void stack_push(Stack<int> stack)
	{
		for (int i = 0; i < 5; i++)
		{
			stack.Push(i);
		}
	}

	// Popping element from the top of the stack
	static void stack_pop(Stack<int> stack)
	{
		Console.WriteLine("Pop :");

		for (int i = 0; i < 5; i++)
		{
			int y = (int)stack.Pop();
			Console.WriteLine(y);
		}
	}

	// Displaying element on the top of the stack
	static void stack_peek(Stack<int> stack)
	{
		int element = (int)stack.Peek();
		Console.WriteLine("Element on stack top : " + element);
	}

	// Searching element in the stack
	static void stack_search(Stack<int> stack, int element)
	{
		bool pos = stack.Contains(element);

		if (pos == false)
			Console.WriteLine("Element not found");
		else
			Console.WriteLine("Element is found at position " + pos);
	}

	// Driver code
	public static void Main(String[] args)
	{
		Stack<int> stack = new Stack<int>();

		stack_push(stack);
		stack_pop(stack);
		stack_push(stack);
		stack_peek(stack);
		stack_search(stack, 2);
		stack_search(stack, 6);
	}
}

// This code contributed by Rajput-Ji

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now