What is Queue : A Queue is a data structure which follows FIFO pattern (First In First Out) means first inserted element will be the first element to be removed from the Queue Here we will see how we can implement queue in java.
Operations of Queue:
- enqueue() : This operation will add element from the rear side of queue.
- dequeue() : This operation will remove element from the front of the queue.
- front() : Returns the element at the front (head)of queue without removing the element.
- Rear(): Returns the element at the rear of the queue(tail) without removing the element.
Java Queue Implementation using Array:
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
public class QueueDemo { private int capacity; int queArr[]; int front = 0; int rear = -1; int queSize = 0; public QueueDemo(int queueSize) { this.capacity = queueSize; queArr = new int[this.capacity]; } //this method inserts element at the end of the queue. public void enqueue(int item) { if (isFull()) { System.out.println("Cannot add element as queue is overflow : "+item); } else { rear++; if(rear == capacity-1) { rear = 0; } queArr[rear] = item; queSize++; System.out.println("Adding element in the queue : " +item); } } //This method removes an element from the top of the queue public void dequeue() { if (isEmpty()) { System.out.println("Underflow ! Cannot remove element as queue is underflow"); } else { front++; if(front == capacity-1) { System.out.println("Element removed from the queue : "+queArr[front-1]); front = 0; } else { System.out.println("Element removed from the queue : "+queArr[front-1]); } queSize--; } } // This method returns true if queue is full otherwise return false public boolean isFull() { boolean status = false; if (queSize == capacity) { status = true; } return status; } //This method returns true if queue is empty otherwise false public boolean isEmpty() { boolean status = false; if (queSize == 0) { status = true; } return status; } // This method returns element at the front of the queue without removing it. public int front() { return queArr[front]; } // This method returns element at the rear of the queue without removing it. public int rear() { return queArr[rear]; } public static void main(String a[]) { QueueDemo queue = new QueueDemo(15); queue.enqueue(34); queue.dequeue(); queue.enqueue(15); queue.enqueue(27); queue.enqueue(21); queue.dequeue(); queue.dequeue(); queue.enqueue(11); queue.dequeue(); queue.enqueue(28); queue.enqueue(14); queue.enqueue(56); queue.dequeue(); System.out.println("Element at the front(head) of the queue is : "+queue.front()); System.out.println("Element at the rear(tail) of the queue is : "+queue.rear()); } } |
OutPut:
Adding element in the queue : 34
Element removed from the queue : 34
Adding element in the queue : 15
Adding element in the queue : 27
Adding element in the queue : 21
Element removed from the queue : 15
Element removed from the queue : 27
Adding element in the queue : 11
Element removed from the queue : 21
Adding element in the queue : 28
Adding element in the queue : 14
Adding element in the queue : 56
Element removed from the queue : 11
Element at the front(head) of the queue is : 28
Element at the rear(tail) of the queue is : 56
Use cases of queue:
1)Queue is used where a resource is shared between multiple users like CPU scheduling.
2) Queue is used in the application where data needs to be transferred asynchronously like JMQ applications.
3) Breadth First search(BFS) algorithm uses queue