C# Program to Implement Stack with Push and Pop Operations
Posted by Superadmin on August 15 2022 07:31:15
C# Program to Implement Stack with Push and Pop Operations
This is a C# Program to implement stack with push and pop operations.
Problem Description
This C# Program Implements Stack with Push and Pop operations.
Problem Solution
Here Push enters an item on the stack, and pop retrieves an item, moving the rest of the items in the stack up one level.
Program/Source Code
Here is source code of the C# Program to Implement Stack with Push and Pop operations. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/*
* C# Program to Implement Stack with Push and Pop operations
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
stack st = new stack();
while (true)
{
Console.Clear();
Console.WriteLine("\nStack MENU(size -- 10)");
Console.WriteLine("1. Add an element");
Console.WriteLine("2. See the Top element.");
Console.WriteLine("3. Remove top element.");
Console.WriteLine("4. Display stack elements.");
Console.WriteLine("5. Exit");
Console.Write("Select your choice: ");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Enter an Element : ");
st.Push(Console.ReadLine());
break;
case 2: Console.WriteLine("Top element is: {0}", st.Peek());
break;
case 3: Console.WriteLine("Element removed: {0}", st.Pop());
break;
case 4: st.Display();
break;
case 5: System.Environment.Exit(1);
break;
}
Console.ReadKey();
}
}
}
interface StackADT
{
Boolean isEmpty();
void Push(Object element);
Object Pop();
Object Peek();
void Display();
}
class stack : StackADT
{
private int StackSize;
public int StackSizeSet
{
get { return StackSize; }
set { StackSize = value; }
}
public int top;
Object[] item;
public stack()
{
StackSizeSet = 10;
item = new Object[StackSizeSet];
top = -1;
}
public stack(int capacity)
{
StackSizeSet = capacity;
item = new Object[StackSizeSet];
top = -1;
}
public bool isEmpty()
{
if (top == -1) return true;
return false;
}
public void Push(object element)
{
if (top == (StackSize - 1))
{
Console.WriteLine("Stack is full!");
}
else
{
item[++top] = element;
Console.WriteLine("Item pushed successfully!");
}
}
public object Pop()
{
if (isEmpty())
{
Console.WriteLine("Stack is empty!");
return "No elements";
}
else
{
return item[top--];
}
}
public object Peek()
{
if (isEmpty())
{
Console.WriteLine("Stack is empty!");
return "No elements";
}
else
{
return item[top];
}
}
public void Display()
{
for (int i = top; i > -1; i--)
{
Console.WriteLine("Item {0}: {1}", (i + 1), item[i]);
}
}
}
}
Program Explanation
In this C# program, we are using a menu driven program to implement stack with push and pop operations. It consists of 1. Add an element, 2. See the Top element, 3. Remove top element, 4. Displays stack elements, 5. Exit using ‘choice’ variable. Pass the value as an argument in switch case statement. In Case 1 statement we are reading the element to add an element into the stack.
The case 2 statement is used to call the peek() function in the top element of the stack. In peek() function if else condition statement is used to check that the stack is empty if the condition is true. Then execute the statement and print the statement as stack is empty. Otherwise, if the condition is false then execute the else statement we are getting the element in the stack.
The case 3 statement is used to call the pop() function to remove the elements. If else condition statement is used to check the stack is empty. If the condition is true, then execute the statement and print the statement as stack is empty. Otherwise, if the condition is false, then decrement the element in the stack.
The case 4 statement is used to print the elements in the stack using display() function. Initialize the value of ‘i’ variable as the value of ‘top’ variable and check the condition the value of ‘i’ variable is greater than -1 value. If the condition is true then execute the iteration of the loop. Here Push enters an item on the stack, and pop retrieves an item, moving the rest of the items in the stack up one level.
Runtime Test Cases
Stack MENU(size -- 10)
1. Add an element
2. See the Top Element
3. Remove the Top Element
4. Display Stack Elements
5. Exit
Select your Choice : 1
Enter the Element : 25
Item Pushed Successfully!
Select your choice :1
Enter the Element : 26
Item Pushed Successfully!
Select your choice : 4
Item 2 :26
Item 1 :25