/* Queue.java */ package mypack; import java.io.*; interface Queue1 { public void initialize(); public void insQueue(); public void delQueue(); public void display(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); } public class Queue implements Queue1 { private int Q[]; private int size; private int front; private int rear; public void initialize() { try { front=-1; rear=-1; System.out.print("Enter size of the queue: "); size=Integer.parseInt(br.readLine()); Q=new int[size]; } catch(Exception e) { System.err.println(e); } } public void insQueue() { if(rear==size-1) System.out.println("\tQueue overflow"); else { try { System.out.print("\tEnter data to insert: "); Q[++rear]=Integer.parseInt(br.readLine()); } catch(Exception e) { System.err.println(e); } } } public void delQueue() { if(front==rear) System.out.println("\tQueue underflow"); else System.out.println("\tDeleted item is: "+Q[++front]); } public void display() { if(front==rear) System.out.println("\tQueue is empty"); else { System.out.print("\tQueue items are: "); for(int i=front+1;i<=rear;i++) System.out.print("\t"+Q[i]); System.out.println(); } } }
After compiling above code, "Queue.class" & "Queue1.class" files will be generated. Create a new folder and name it as "mypack" in the same location of your program and copy the "Queue.class" & "Queue1.class" files in the folder.
Place the below code outside "mypack" and run it
/* Test.java */ import mypack.Queue; import java.io.*; class Main { public static void main(String[] args) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Queue ob=new Queue(); ob.initialize(); try { int exit=0; while(exit==0) { System.out.println("1.Insert\t2.Delete\t3.DISPLAY\t4.EXIT"); System.out.print("\tEnter your choice: "); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1:ob.insQueue(); break; case 2:ob.delQueue(); break; case 3:ob.display(); break; case 4:exit=1; break; default:System.out.println("Wrong choice given"); } } } catch(Exception e) { System.err.println(e); } } }
Output |
No comments:
Post a Comment