AdBlock Detected!

Our website is made possible by displaying ads to our visitors. Please support us by whitelisting our website.

Computer Neek

What is a Queue

A queue in computer science is a collection of items that are added from the bottom to the top and removed from the bottom to the top. This is commonly referred to as first in first out(FIFO).

There are five operations of a stack

enqueue(data) - add data
dequeue() - remove data
is_empty()
is_full()

Queues

Arrays are commonly used to operate queues. Assume we have an array with a size that can hold three items and we enqueue()(add) three items. Item 1 will have the name "Steve," item 2 will have the name "Julian," and item 3 will have the name "Sarah." Steve will be at the front of the queue because he was the first item added. Sarah would be at the back of the queue because she was the last person to be added to it. The front of the Queue has the value 1 because it is the first item in the queue, and the back has the value 3 because it is the last item added to the queue.

image of a queue

If you were to dequeue() an item from this queue, steve would be removed and the front would increment. The rear would still be Sarah at 3.

image of a priority queue

Priority Queue

A priority Queue differs slightly in that each item in the queue has a priority. When we add new items to the queue, if they have a higher priority than some of those already in the queue, they are placed ahead of those with a lower priority. A hierarchical system is an example. A CEO may have a higher priority than the manager thus, their needs will be prioritised over the managers'. Managers' needs may be prioritised higher than those of regular employees, but not as high as those of the CEO. A way to show this may be an each person having a specific Key. If you have the letter A as a key, you have the highest priority but if you have the letter C as a key, you have a lower priority.

Circular Queue

Consider the example under the heading Queues. The problem with the above queue is that the rear value has reached its capacity, but there is still one space available to fill the queue. We could dequeue() all of the items and then repopulate them, but this is inefficient. Circular queues are useful in this situation. In circular queue's when the rear has reached its maximum, it is moved to the first element of the array and the empty first element is populated from there. So, in the above example, if we wanted to add "James" to our queue, the rear would move from item 3, back to item 1, and populate that array with James. This means that the rear is now 1 and the front is still "Julian"(item 2). The next item to be dequeue() would be "Julain".

image of a circular queue

This can also been shown in a circle

To read about stacks click here or to read about different topics, click here