Sie sind auf Seite 1von 4

5.

Write a program to impement Stack Operations in C#

using System;
using System.Collections.Generic;
using System.Text;

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)
{
//Console.WriteLine("top={0}", top);
if (top == (max - 1))
{
Console.WriteLine("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)
{
Console.WriteLine("stack is underflow");
return;
}
else
Console.Write("The popped elements are:{0}\t", a[top]);
top--;

//Displaying the current elements from the stack


public void display()
{
if (top == -1)
{
Console.WriteLine("stack is underflow");
return;
}
Console.Write("The elements in the stack are:");
for (i = 0; i <= top; i++)
{
Console.Write("{0}\t", a[i]);
}
}
}

2
5. Stack Operations : continuation

class stack1
{
static void Main(string[] args)
{
stack s = new stack();
int choice, n;
Console.WriteLine("PROGRAM TO IMPLEMENT STACK OPERATIONS");
do
{
Console.WriteLine("\n\n1.Insertion");
Console.WriteLine("2.Pop");
Console.WriteLine("3.Display");
Console.WriteLine("4.Quit");
Console.WriteLine("Enter your choice");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:

Console.WriteLine("Enter the value to insert in the stack");


n = int.Parse(Console.ReadLine());
s.insert(n);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
Console.WriteLine("you are exited");
Console.ReadLine();
Environment.Exit(0);
break;
default:
Console.WriteLine("Enter valid choice");
break;
}
} while (choice!= 4);

}
}
}

3
4

Das könnte Ihnen auch gefallen