Stack main class
import java.io.*;
class stack{
public static void main(String args[]) throws IOException
{
stackblue stacker=new stackblue();
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
int c=0;
do{
System.out.println("1 : to print elements in stack");
System.out.println("2 : Current Status");
System.out.println("3 : to push");
System.out.println("4 : To pop");
System.out.println("5 : Exit");
System.out.println("\n Enter Your Choice");
c=Integer.parseInt(br.readLine());
if(c==1){
stacker.printElements();
}
if(c==2){
stacker.currentStatus();
}
if(c==3){
stacker.push();
}
if(c==4){
stacker.pop();
}
}
while(c!=5);
}
}//class closed
Stack Main Logic
import java.io.*;
class stackblue{
// global variables
int current_position=-1; // start position of stack
int size=20; // Size of stack
int arr[] = new int[size]; // create an array of size 'size'
// create object to take input from user
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//method to push element in stack
void push()throws IOException{
if(!full()){
System.out.println("Enter the Element");
arr[current_position+1]=Integer.parseInt(br.readLine());
current_position++;
}
else{
}
}
// method to pop the element from stack
void pop()throws IOException{
if(!empty()){
System.out.println("Poped Element is : " + arr[current_position]);
current_position--;
}
else {
}
}
// method to get current Status of stack
void currentStatus(){
if(!empty() && !full()){
System.out.println("Empty positions : " + ((size-current_position)-1));
}
else{
}
}
// method to print elements
void printElements(){
if(empty() || full()){
return;
}
for(int j=current_position;j>=0;j--)
{ System.out.print(arr[j] + ",");
}
System.out.println();
}
// method to check if stack is empty
boolean empty(){
if((current_position)==-1){
System.out.println("Stack is Empty");
return true;
}
else{
return false;
}
}
// method to check if stack is full
boolean full(){
if((current_position)==(size-1)){
System.out.println("Stack is Full");
return true;
}
else{
return false;
}
}
}