

Each item in the queue must have a priority associated with it.Characteristics of a Priority QueueĪ queue is called a Priority Queue when it has the following characteristics: (in such a way that the highest priority elements are moved to the beginning of the queue and the lowest priority elements are moved to the back of the queue). Hence, the items in the priority queue are arranged in either ascending or descending order depending on the priority value taken by the user. However, in a Priority Queue, each item in the queue is assigned a priority, and items are dequeued or removed based on this priority value assigned to them. Just like any queue we form, the person that stands first in the queue, will be addressed first and similarly, even in computer science queues, the same rule applies. In a normal Queue, first-in-first-out is followed which means that whichever elements enters the queue first, will be the first to leave.

It is an Abstract Data Type that functions a little differently than a normal Queue. if the two elements are a and b, they should follow one of the conditions:Īs a result, the items in the priority queue are arranged in ascending or descending order. Elements are called comparable if they are either lesser than, equal to or greater than one another, i.e. It is because they use fixed memory and utilize no memory beyond that.It is important to note that a priority queue only supports elements that are comparable. The Space complexity of all operations is also the same. We find that they all have the same time complexity of O(1) as they are using only one operation to execute the methods. In this tutorial, we analyze the time and space complexities of array queues for enQueue(), dequeue (), peek(), and isempty() operations. It checks the elements of the Queue, that is constant.

Example 1Īnalyzing time and Space Complexities of different Queue Operations in C/C++ #include Space Complexity − It is the memory utilization. Time Complexity − It is the amount of time used to execute. Peek() − Returns the peak value of the queue. IsEmpty() − It will check whether the queue is empty or not.

The important functions of the queue areĮnQueue() − It is used to insert data in the Queue.ĭeQueue() − It is used to remove data from the Queue. In an array based queue, the queue size is defined initially. We can implement a Queue using array and linked-list. The real-life example of Queue is a Queue in front of the ticket window. Queue elements are removed from the Front end. Its elements are inserted at the Rear end. The principle of Queue is its FIFO approach and it states that the element that enters first in the Queue will be the first to be removed from it. In this tutorial, we will analyze the time and space complexity of array based queue for its different operation. It can be implemented by using arrays and linked lists. Queue is a linear data structure that uses the FIFO approach for inserting and removing its elements.
