Stack main class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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; } } } |