Que 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 |
import java.io.*; class que{ public static void main(String args[]) throws IOException{ // create an object to take input from user BufferedReader br= new BufferedReader (new InputStreamReader(System.in)); // create a QueBlueprint object QueBlueprint Quer = new QueBlueprint(); //take choices from user int c=0; do{ System.out.println("1 : to print elements in queue"); System.out.println("2 : Current Status"); System.out.println("3 : To Add Element (Push)"); System.out.println("4 : To Remove Element (Pop)"); System.out.println("5 : Exit"); System.out.println("\n Enter Your Choice"); c=Integer.parseInt(br.readLine()); if(c==1){ Quer.printQueue(); } if(c==2){ Quer.currentStatus(); } if(c==3){ Quer.push(); } if(c==4){ Quer.pop(); } } while(c!=5); } } |
Queue 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import java.io.*; class QueBlueprint{ // declare some global variables int currentPosition= -1; int size= 20; int arr[]= new int[size]; BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); //method to check if queue is full protected boolean full(){ if(currentPosition==(size-1)){ System.out.println("Queue is Full"); return true; } else{ return false; } } // method to check if queue is empty protected boolean empty(){ if(currentPosition != -1){ return false; } else{ System.out.println("Queue is Empty"); return true; } } // method to print the queue void printQueue(){ if(!full() && !empty()){ for(int i=0;i<=currentPosition;i++){ System.out.print(arr[i]+ ","); } System.out.println(""); } } // method to save new element to queue void push() throws IOException{ if(!full()){ System.out.print("Enter the element to be pushed : "); arr[currentPosition+1]=Integer.parseInt(br.readLine()); currentPosition++; System.out.println(); } else{ } } // method to pop the element from the queue void pop(){ if(!empty()){ System.out.println("The element poped out was : " + arr[0]); // shift all elements one place shiftQueue(); currentPosition--; } else{} } // method to shift the elements of queue protected void shiftQueue(){ if(currentPosition>0){ for(int i=0;i<currentPosition;i++){ arr[i]=arr[i+1]; } } } // method to print the current status of queue void currentStatus(){ if(full() || empty()){ } else{ System.out.println("There are Currently : " + ((size-currentPosition)-1) + " positions empty"); } } } |