5.
Write a program to impement Stack Operations in C#
using System;
using [Link];
using [Link];
namespace LABPROGRAMS
{
class stack
{
int i, max, top;
int[] a = new int[10];
public stack()
{
top = -1;
max = 5;
}
//Inserting an elements into the stack
public void insert(int item)
{
//[Link]("top={0}", top);
if (top == (max - 1))
{
[Link]("stack is overflow");
return;
}
else
top++;
a[top] = item;
1
5. Stack Operations : continuation
//Deleting an elements from the stack
public void pop()
{
if (top == -1)
{
[Link]("stack is underflow");
return;
}
else
[Link]("The popped elements are:{0}\t", a[top]);
top--;
//Displaying the current elements from the stack
public void display()
{
if (top == -1)
{
[Link]("stack is underflow");
return;
}
[Link]("The elements in the stack are:");
for (i = 0; i <= top; i++)
{
[Link]("{0}\t", a[i]);
}
}
}
2
5. Stack Operations : continuation
class stack1
{
static void Main(string[] args)
{
stack s = new stack();
int choice, n;
[Link]("PROGRAM TO IMPLEMENT STACK OPERATIONS");
do
{
[Link]("\n\[Link]");
[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");
[Link]("Enter your choice");
choice = [Link]([Link]());
switch (choice)
{
case 1:
[Link]("Enter the value to insert in the stack");
n = [Link]([Link]());
[Link](n);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]("you are exited");
[Link]();
[Link](0);
break;
default:
[Link]("Enter valid choice");
break;
}
} while (choice!= 4);
}
}
}
3
4