import java.io.*;
class Stack
{
int arr[]=new int[10];
int len=0;
Stack()
{
for(int i=0;i<10;i++)
arr[i]=0;
}
void push(int a)
{
if(len<10)
{
arr[len]=a;
len++;
}
else System.out.println("Cannot Input more than 10 Numbers");
}
int pop()
{
if(len>0)
return arr[len-1];
else return -1;
}
int popAndTop()
{
if(len>0)
return arr[len--];
else return -1;
}
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
Stack pile=new Stack();
do
{
System.out.println("\tList\n1.Push\n2.Pop\n3.Pop And Top\n4.Exit");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:System.out.println("Enter Element");
int n=Integer.parseInt(br.readLine());
pile.push(n);
break;
case 2:if(pile.pop()!=-1)
System.out.println("\t"+pile.pop());
else System.out.println("No Elements to Display");
break;
case 3:if(pile.popAndTop()!=-1)
System.out.println("\t"+pile.popAndTop());
else System.out.println("No Elements to Display");
break;
case 4:System.out.println("Goodbye");break;
default:System.out.println("Invalid choice");
}
}while(ch!=4);
}
}
No comments:
Post a Comment