Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).  A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

Operations on Queue:
Mainly the following four basic operations are performed on queue:

Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.
Front: Get the front item from queue.
Rear: Get the last item from queue.

Queue-Data Structure-Introduction and Array Implementation

Applications of Queue:
Queue is used when things don’t have to be processed immediatly, but have to be processed in First InFirst Out order like Breadth First Search. This property of Queue makes it also useful in following kind of scenarios.

1) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.

See this for more detailed applications of Queue and Stack.

[ad type=”banner”]

Array implementation Of Queue
For implementing queue, we need to keep track of two indices, front and rear. We enqueue an item at the rear and dequeue an item from front. If we simply increment front and rear indices, then there may be problems, front may reach end of the array. The solution to this problem is to increase front and rear in circular manner (See this for details)

Java  Programming

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20for%20array%20implementation%20of%20queue%0A%20%20%0A%2F%2F%20A%20class%20to%20represent%20a%20queue%0Aclass%20Queue%0A%7B%0A%20%20%20%20int%20front%2C%20rear%2C%20size%3B%0A%20%20%20%20int%20%20capacity%3B%0A%20%20%20%20int%20array%5B%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20public%20Queue(int%20capacity)%20%7B%0A%20%20%20%20%20%20%20%20%20this.capacity%20%3D%20capacity%3B%0A%20%20%20%20%20%20%20%20%20front%20%3D%20this.size%20%3D%200%3B%20%0A%20%20%20%20%20%20%20%20%20rear%20%3D%20capacity%20-%201%3B%0A%20%20%20%20%20%20%20%20%20array%20%3D%20new%20int%5Bthis.capacity%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Queue%20is%20full%20when%20size%20becomes%20equal%20to%20%0A%20%20%20%20%2F%2F%20the%20capacity%20%0A%20%20%20%20boolean%20isFull(Queue%20queue)%0A%20%20%20%20%7B%20%20return%20(queue.size%20%3D%3D%20queue.capacity)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Queue%20is%20empty%20when%20size%20is%200%0A%20%20%20%20boolean%20isEmpty(Queue%20queue)%0A%20%20%20%20%7B%20%20return%20(queue.size%20%3D%3D%200)%3B%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20add%20an%20item%20to%20the%20queue.%20%0A%20%20%20%20%2F%2F%20It%20changes%20rear%20and%20size%0A%20%20%20%20void%20enqueue(%20int%20item)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(isFull(this))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20this.rear%20%3D%20(this.rear%20%2B%201)%25this.capacity%3B%0A%20%20%20%20%20%20%20%20this.array%5Bthis.rear%5D%20%3D%20item%3B%0A%20%20%20%20%20%20%20%20this.size%20%3D%20this.size%20%2B%201%3B%0A%20%20%20%20%20%20%20%20System.out.println(item%2B%20%22%20enqueued%20to%20queue%22)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20remove%20an%20item%20from%20queue.%20%20%0A%20%20%20%20%2F%2F%20It%20changes%20front%20and%20size%0A%20%20%20%20int%20dequeue()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(isEmpty(this))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Integer.MIN_VALUE%3B%0A%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%0A%20%20%20%20%20%20%20%20this.front%20%3D%20(this.front%20%2B%201)%25this.capacity%3B%0A%20%20%20%20%20%20%20%20this.size%20%3D%20this.size%20-%201%3B%0A%20%20%20%20%20%20%20%20return%20item%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20get%20front%20of%20queue%0A%20%20%20%20int%20front()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(isEmpty(this))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Integer.MIN_VALUE%3B%0A%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%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%20%0A%20%20%20%20%2F%2F%20Method%20to%20get%20rear%20of%20queue%0A%20%20%20%20int%20rear()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(isEmpty(this))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Integer.MIN_VALUE%3B%0A%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%0A%20%20%20%20%7D%0A%7D%0A%20%20%0A%20%20%20%0A%2F%2F%20Driver%20class%0Apublic%20class%20Test%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Queue%20queue%20%3D%20new%20Queue(1000)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20queue.enqueue(10)%3B%0A%20%20%20%20%20%20%20%20queue.enqueue(20)%3B%0A%20%20%20%20%20%20%20%20queue.enqueue(30)%3B%0A%20%20%20%20%20%20%20%20queue.enqueue(40)%3B%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(queue.dequeue()%20%2B%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%20queue%5Cn%22)%3B%0A%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%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%20queue.front())%3B%0A%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%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%20queue.rear())%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%0A” message=”” highlight=”” provider=”manual”/]

Output:

10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
10 dequeued from queue
Front item is 20
Rear item is 40

Time Complexity: Time complexity of all operations like enqueue(), dequeue(), isFull(), isEmpty(), front() and rear() is O(1). There is no loop in any of the operations.

[ad type=”banner”]

Categorized in: