Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. /* HW5
  2. * Due: 22 October 2017
  3. * Problem Header Hash Code: bfd416aa02529df87ee21caadec5ab81
  4. */
  5. package hw5;
  6.  
  7. public class Queue {
  8. Node[] arr; // circular Queue
  9. int capacity;
  10. int front;
  11. int back;
  12. int size;
  13.  
  14. public Queue(int cap){
  15. arr = new Node[cap];
  16. capacity = cap;
  17. }
  18.  
  19. public void enqueue(Node node){
  20. if (!isFull()){
  21. if(back < capacity-1){
  22. arr[back] = node;
  23. back++;
  24. }else{
  25. back = 0;
  26. arr[back] = node;
  27. }size++;
  28. }else{
  29. System.out.println("Queue Overflow!!!");
  30. }
  31. }
  32.  
  33. public Node dequeue(){
  34. if (!isEmpty()){
  35. Node keep = arr[front];
  36. while(arr[front]==null){
  37. front++;
  38. }
  39. arr[front] = null;
  40. front++;
  41. //if(front==capacity) front=0;
  42. size--;
  43. return keep;
  44. }else{
  45. System.out.println("Queue Underflow!!!");
  46. return null;
  47. }
  48. }
  49.  
  50. public boolean isEmpty(){
  51. boolean check = false;
  52. if(size == 0){
  53. check = true;
  54. }
  55. return check;
  56. }
  57.  
  58. public boolean isFull(){
  59. boolean check2 = false;
  60. if(size == capacity){
  61. check2 = true;
  62. }
  63. return check2;
  64. }
  65.  
  66. public void printCircularIndices(){
  67. System.out.println("Front index = " + front + " Back index = " + back);
  68. }
  69.  
  70. public void printQueue(){
  71. if (!isEmpty()){
  72. System.out.print("[Front] ");
  73. if(back>front){
  74. for(int i=front;i<back;i++){
  75. System.out.print(arr[i].data);
  76. }
  77. }else{
  78. for(int i=0;i<front;i++){
  79. System.out.print(arr[i].data);
  80. }for(int i=back;i<capacity;i++){
  81. System.out.print(arr[i].data);
  82. }
  83. }
  84. System.out.println("[Back]");
  85. }else{
  86. System.out.println("Empty Queue!!!");
  87. }
  88.  
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement