Java program to perform stack operation using interface...
Java Code:
8)import java.util.Scanner;
interface stk
{
void pop();
void disp();
void push();
}
class stack implements stk
{
int[] a=new int[10];
int i=0,k;
public void push()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number");
a[i]=in.nextInt();
i++;
}
public void pop()
{
int j=i;
j--;
if(j<0)
{
System.out.println("Stack is empty");
i++;
}
else
{
System.out.println("last value popped out");
i=j;
}
}
public void disp()
{
int v=i;
v--;
if(v>=0)
{
System.out.println("*****Display*****");
for(k=v;k>=0;k--)
{
System.out.println(a[k]);
}
}
else
{
System.out.println("Stack is empty");
}
}
}
public class prgr8
{
public static void main(String[] ar)
{
int ch;
stack s=new stack();
System.out.println("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");
do
{
Scanner in=new Scanner(System.in);
System.out.println("Select the option");
ch=in.nextInt();
switch(ch)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.disp();
break;
}
}
while(ch!=4);
}
}
OUTPUT:
C:\Users\PRIYA\Desktop\java>javac prgr8.java
C:\Users\PRIYA\Desktop\java>java prgr8
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Select the option
1
Enter a number
34
Select the option
1
Enter a number
456
Select the option
1
Enter a number
345
Select the option
3
*****Display*****
345
456
34
Select the option
2
last value popped out
Select the option
3
*****Display*****
456
34
Select the option
4
2.POP
3.DISPLAY
4.EXIT
Select the option
1
Enter a number
34
Select the option
1
Enter a number
456
Select the option
1
Enter a number
345
Select the option
3
*****Display*****
345
456
34
Select the option
2
last value popped out
Select the option
3
*****Display*****
456
34
Select the option
4
Comments
Post a Comment