Blogger Widgets

Total Page visits

Wednesday, April 24, 2013

Program in C# to implement Stack operations



using System;
class Stack
{
public static void Main(String[] args)
{
int top=-1,num,choice,max,rpt=1;
int[] stack=new int[20];
Console.WriteLine(“Enter the maximum limit”);
max=int.Parse(Console.ReadLine());
while(rpt!=0)
{
Console.WriteLine(“Stack Operation”);
Console.WriteLine(“1 . PUSH”);
Console.WriteLine(“2 . POP”);
Console.WriteLine(“3 . DISPLAY”);
Console.WriteLine(“0 . EXIT”);
Console.WriteLine(“Enter your choice:”);
choice=int.Parse(Console.ReadLine());
switch(choice)
{
case 1:
if(top==(max-1)) {
Console.WriteLine(“Stack is full”);
}
else {
Console.WriteLine(“Enter the element to be inserted:”);
num=int.Parse(Console.ReadLine());
stack[++top]=num;
Console.WriteLine(“Element is successfully inserted”);
}
break;
case 2:
if(top==-1)
{
Console.WriteLine(“Stack is Empty”);
}
else {
Console.WriteLine(“Deleted Element is :”+stack[top--]);
}
break;
case 3:
if(top==-1)
{
Console.WriteLine(“Stack is Empty”);
}
else {
Console.WriteLine(“Elements in the stack”);
for(int i=top;i>=0;i–)
{
Console.WriteLine(stack[i]);
}
}
break;
case 0:
exit(0);
break;
default:
Console.WriteLine(“Worng Entery”);
break;
}
}
}
}

No comments: