0% found this document useful (0 votes)
3 views2 pages

Stackop Test Dedt

Tedt

Uploaded by

cadillacvua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Stackop Test Dedt

Tedt

Uploaded by

cadillacvua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical-1.4: Program to perform Push and Pop Stack Operations.

import java.util.Scanner;
class stackop
{

int max=10;
int top=0;
int[] a= new int[max];
void push(int x)
{
if(top==max-1)
{
System.out.println("Overflow");
}
else
{
a[top]=x;
top++;
}
}
int pop()
{
if(top<0)
{
System.out.println("Underflow");
return -1;
}
else{
top--;
return(a[top+1]);
}

}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
stackop stk= new stackop();
int choice=0;
int x;
while(choice<3)
{
System.out.println("1.Push\n2.Pop\n3.Exit");
choice=scan.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the element u want to push:");
x=scan.nextInt();
stk.push(x);
break;
case 2:
if(stk.pop()!=-1)
{
System.out.println(stk.pop());
}
break;
case 3:
break;
}
}
}
}

Output:

1.Push
2.Pop
3.Exit
1
Enter the element u want to push:
2
1.Push
2.Pop
3.Exit
2
1.Push
2.Pop
3.Exit
2
Underflow
1.Push
2.Pop
3.Exit
3

You might also like