Guest User

Untitled

a guest
Jun 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import java.util.Arrays;
  2. import queue.QueueInterface;
  3.  
  4. /*
  5. * To change this license header, choose License Headers in Project Properties.
  6. * To change this template file, choose Tools | Templates
  7. * and open the template in the editor.
  8. */
  9.  
  10. /**
  11. *
  12. * @author Student
  13. * @param <T>
  14. */
  15. public class Queue <T> implements QueueInterface <T> {
  16.  
  17. int head;
  18. int tail;
  19. int size;
  20. T[] queue;
  21. int capacity;
  22. boolean full;
  23. //int last;
  24.  
  25. public Queue(int capacity){
  26. queue = (T[]) new Object[capacity];
  27. this.size = 0;
  28. this.head = 0;
  29. this.tail = -1;
  30. this.capacity = capacity;
  31. }
  32. @Override
  33. public void enqueue(T t) {
  34.  
  35. this.tail++;
  36.  
  37. if (tail == capacity && head!=0){
  38. this.tail = (tail)%capacity;
  39. }
  40.  
  41. if(!isFull()){
  42. queue[tail] = t;
  43. }
  44. else{
  45. expand();
  46. enqueue(t);
  47. }
  48. this.size++;
  49. }
  50.  
  51. @Override
  52. public T dequeue() {
  53. if(head!=capacity-1){
  54. this.head = head++;
  55. this.size = size--;
  56. }
  57. else{
  58. this.head = 0;
  59. }
  60. return queue[head];
  61. }
  62.  
  63. @Override
  64. public T getFront() {
  65. return this.queue[head];
  66. }
  67.  
  68. public boolean isFull(){
  69. if(capacity == size){
  70. full = true;
  71. }
  72. return full;
  73. }
  74.  
  75. public void expand(){
  76. this.capacity = capacity*2;
  77. this.queue = Arrays.copyOf(queue, capacity);
  78. }
  79. /**
  80. *
  81. * @return
  82. */
  83. @Override
  84. public int getLength(){
  85. return this.size;
  86. }
  87.  
  88. }
Add Comment
Please, Sign In to add comment