- Array is the easiest way to implement a queue. Queue can be also implemented using Linked List or Stack.
- Front and Rear of the queue point at the first index of the array. (Array index starts from 0).
- While adding an element into the queue, the Rear keeps on moving ahead and always points to the position where the next element will be inserted. Front remains at the first index.
Sample Code
[pastacode lang=”javascript” manual=”import%20java.io.*%3B%0Aclass%20Queue%20%0A%7B%20%0A%20%20%20%20int%20front%2C%20rear%2C%20size%3B%20%0A%20%20%20%20int%20%20capacity%3B%20%0A%20%20%20%20int%20array%5B%5D%3B%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20public%20Queue(int%20capacity)%20%7B%20%0A%20%20%20%20%20%20%20%20%20this.capacity%20%3D%20capacity%3B%20%0A%20%20%20%20%20%20%20%20%20front%20%3D%20this.size%20%3D%200%3B%20%20%0A%20%20%20%20%20%20%20%20%20rear%20%3D%20capacity%20-%201%3B%20%0A%20%20%20%20%20%20%20%20%20array%20%3D%20new%20int%5Bthis.capacity%5D%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Queue%20is%20full%20when%20size%20becomes%20equal%20to%20%20%0A%20%20%20%20%2F%2F%20the%20capacity%20%20%0A%20%20%20%20boolean%20isFull(Queue%20queue)%20%0A%20%20%20%20%7B%20%20return%20(queue.size%20%3D%3D%20queue.capacity)%3B%20%0A%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Queue%20is%20empty%20when%20size%20is%200%20%0A%20%20%20%20boolean%20isEmpty(Queue%20queue)%20%0A%20%20%20%20%7B%20%20return%20(queue.size%20%3D%3D%200)%3B%20%7D%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20add%20an%20item%20to%20the%20queue.%20%20%0A%20%20%20%20%2F%2F%20It%20changes%20rear%20and%20size%20%0A%20%20%20%20void%20enqueue(%20int%20items)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20if%20(isFull(this))%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%20%0A%20%20%20%20%20%20%20%20this.rear%20%3D%20(this.rear%20%2B%201)%25this.capacity%3B%20%0A%20%20%20%20%20%20%20%20this.array%5Bthis.rear%5D%20%3D%20items%3B%20%0A%20%20%20%20%20%20%20%20this.size%20%3D%20this.size%20%2B%201%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(items%2B%20%22%20enqueued%20to%20the%20queue%22)%3B%20%0A%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20remove%20an%20item%20from%20queue.%20%20%20%0A%20%20%20%20%2F%2F%20It%20changes%20front%20and%20size%20%0A%20%20%20%20int%20dequeue()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20if%20(isEmpty(this))%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Integer.MIN_VALUE%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20int%20item%20%3D%20this.array%5Bthis.front%5D%3B%20%0A%20%20%20%20%20%20%20%20this.front%20%3D%20(this.front%20%2B%201)%25this.capacity%3B%20%0A%20%20%20%20%20%20%20%20this.size%20%3D%20this.size%20-%201%3B%20%0A%20%20%20%20%20%20%20%20return%20item%3B%20%0A%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20get%20front%20of%20queue%20%0A%20%20%20%20int%20front()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20if%20(isEmpty(this))%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Integer.MIN_VALUE%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20this.array%5Bthis.front%5D%3B%20%0A%20%20%20%20%7D%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20get%20rear%20of%20queue%20%0A%20%20%20%20int%20rear()%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20if%20(isEmpty(this))%20%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Integer.MIN_VALUE%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20this.array%5Bthis.rear%5D%3B%20%0A%20%20%20%20%7D%20%0A%7D%20%0A%20%20%20%0A%20%20%20%20%0A%2F%2F%20Driver%20class%20%0Apublic%20class%20Test%20%0A%7B%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20Queue%20q1%20%3D%20new%20Queue(1000)%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20q1.enqueue(7)%3B%20%0A%20%20%20%20%20%20%20%20q1.enqueue(8)%3B%20%0A%20%20%20%20%20%20%20%20q1.enqueue(18)%3B%20%0A%20%20%20%20%20%20%20%20q1.enqueue(25)%3B%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(q1.dequeue()%20%2B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20dequeued%20from%20the%20queue%5Cn%22)%3B%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Front%20item%20is%20%22%20%2B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q1.front())%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Rear%20item%20is%20%22%20%2B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q1.rear())%3B%20%0A%20%20%20%20%7D%20%0A%7D” message=”” highlight=”” provider=”manual”/]
Output
[pastacode lang=”markdown” manual=”7%20enqueued%20to%20the%20queue%0A8%20enqueued%20to%20the%20queue%0A18%20enqueued%20to%20the%20queue%0A12%20enqueued%20to%20the%20queue%0A7%20dequeued%20from%20the%20queue%0A%0AFront%20item%20is%208%0ARear%20item%20is%2025″ message=”” highlight=”” provider=”manual”/]