Advertisement
Guest User

Untitled

a guest
May 1st, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public class Queue //stating class
  2. {
  3. int front , rear ,e, queSize , queArr[]; // initialized
  4.  
  5.  
  6. Queue(int size) //constructor
  7. {
  8. this.queSize=size;
  9. this.queArr=new int[queSize];
  10. this.front=-1;
  11. this.rear=-1;
  12.  
  13. }
  14.  
  15. public void EnQueue(int e) //method for input data in Queue
  16. {
  17. if (front==-1) //check Queue is empety or not
  18. front=0; // if yes then initialized it with zero
  19.  
  20. if(rear==(queSize-1)) // check Queue is full or not
  21. System.out.println("overflow"); // if full the over flow
  22.  
  23. else
  24. rear=rear+1;
  25. queArr[rear]=e; // add data in rear of Queue
  26. }
  27.  
  28. public int DeQueue() // method of delete data from Queue
  29. {
  30. if((front==-1) || (front>rear)) // check Queue either empety or "rear is greater then front"
  31. System.out.println("underflow"); //if yes then show underflow
  32.  
  33. else
  34. e =queArr[front]; // delete data from Queue
  35. front=front+1;
  36.  
  37. return e; // value return to method
  38.  
  39. }
  40.  
  41. public void display() // method of display the Queue
  42. {
  43. System.out.println("The data in the Queue");
  44. for(int i=front; i<=rear; i++) // start loop from front to rear
  45. System.out.println(queArr[i]);
  46.  
  47. }
  48.  
  49. public static void main(String args[])
  50. {
  51. Queue obj=new Queue(5); // you will change the size of array
  52. obj.EnQueue(1111); //input data
  53. obj.EnQueue(2222); //input data
  54. obj.EnQueue(3333); //input data
  55. obj.EnQueue(4444); //input data
  56. obj.EnQueue(5555); //input data
  57. obj.display();
  58.  
  59. obj.DeQueue(); //delete data
  60. obj.DeQueue(); //delete data
  61. obj.display();
  62.  
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69. } // end of Queue class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement