A queue is a data structure to which we can insert data and also delete from it. Unlike stack, which follows the LIFO principle ( Last In First Out ), queue implements the FIFO principle ( First In First Out ). In a queue of people, the first person to enter the line is also the first person to exit from it. Similarly, the first item pushed inside a queue is also the first item to be popped from the queue. In this article, we shall look into a queue and implement the peek function in the python queue.
Operations inside a queue
Two main operations can be performed on a queue – enqueue and dequeue. Before understanding the two operations, we need first to understand ‘front’ and ‘rear’ in a queue. ‘Front’ and ‘Rear’ are indexes to the first and last element of the queue, respectively. We use ‘Front’ when we want to delete an element from the queue and ‘Rear’ when we want to insert an element from the queue.
- Enqueue : The enqueue operation is used to add an item to the queue. Before performing an enqueue operation, we will have to check whether the queue is full or not. Only if the queue is has space, we will conduct an enqueue operation. To perform enqueue operation, we have to use ‘Rear’ as new elements are added from the rear end of the queue.
- Dequeue : The dequeue operation is used to pop elements from the queue. While performing dequeue, we will first check if the queue is empty or not. If the queue is empty, then we shall specify an ‘Underflow’ condition else we shall pop an element. To preform dequeue operation, we have to use the ‘Front’ to access the topmost element from the queue in order to pop it.
Queue Using a Class
We will define a queue using a class. We shall define inside the class initially the two operations – enqueue and dequeue and a print function to print the queue.
The name of the class is ‘Queue’. First, we shall define the __init__() function, which takes two parameters – self and size of the queue. We use a list named ‘queue’ to represent the queue. The list ‘queue’ is defined with None values, and its size is the size of the queue.
Initially, the front and rear are set to 0. They represent the index of the first and the last element in the list queue, respectively. We also have an ‘available’ variable which represents the amount of available spaces inside the queue. Since the queue is empty at the beginning, ‘available’ is set to the size of the queue.
class Queue: def __init__(self,size): self.queue = [None]*size self.front = 0 self.rear = 0 self.size = size self.available = size def enqueue(self, item): if self.available == 0: print('Queue Overflow!') else: self.queue[self.rear] = item self.rear = (self.rear + 1) % self.size self.available -= 1 def dequeue(self): if self.available == self.size: print('Queue Underflow!') else: self.queue[self.front] = None self.front = (self.front + 1) % self.size self.available += 1 def print_queue(self): print(self.queue)
The enqueue function takes two arguments – self and the item to be inserted in the queue. First, we check whether the queue contains any available space or not. If self.available is zero, then we would print ‘Queue Overflow’. Otherwise, using the ‘rear’ as in index, we shall assign the value ‘item’ at that index. Then, we shall increment ‘rear’ by one and decrement ‘available’ by one.
The dequeue function takes one argument, which is self. First, we shall check if the self.available is equal to self.size or not. If it is, then it means that the queue is empty, and so it will print Queue Underflow. Else, we would assign the queue item at the ‘front’ index back to ‘None’. Then, we shall increment self.front and self.available by one.
We also have a print_queue() function for printing the queue.
Now, we shall create a queue class object named ‘queue1’. To that, we shall first add two items – 10 and 20 and then perform a dequeue operation once. Then, we shall print the queue. It will only contain one element – 20 at the second position.
queue1 = Queue(4) queue1.enqueue(10) queue1.enqueue(20) queue1.dequeue() queue1.print_queue()
[None, 20, None, None]
Peek function in queue
A peek function in the python queue is used to print the first element of the queue. It returns the item which is present at the front index of the queue. It will not remove the first element but print it.
Let us construct a function inside the class queue to perform a peek operation. The name of the function is ‘peek’, and it accepts only one argument – self. Inside the function body, we shall print the list queue with index as ‘self.front’.
def peek(self): print(self.queue[self.front])
To call the function, we shall use queue1.peek() syntax.
The entire code for the queue class is:
class Queue: def __init__(self,size): self.queue = [None]*size self.front = 0 self.rear = 0 self.size = size self.available = size def enqueue(self, item): if self.available == 0: print('Queue Overflow!') else: self.queue[self.rear] = item self.rear = (self.rear + 1) % self.size self.available -= 1 def dequeue(self): if self.available == self.size: print('Queue Underflow!') else: self.queue[self.front] = None self.front = (self.front + 1) % self.size self.available += 1 def peek(self): print(self.queue[self.front]) def print_queue(self): print(self.queue) queue1 = Queue(4) queue1.enqueue(10) queue1.peek() queue1.enqueue(20) queue1.dequeue() queue1.peek() queue1.print_queue()
The output is:
10 20 [None, 20, None, None]
How to add peek function to priority queue?
A priority queue is a type of queue where every element has a priority value associated with it. Unlike a normal queue where the FIFO principle is implemented, in the case of a priority queue, the values are removed based on their priority.Item with the lowest value is at the ‘front’ end, and the item with the highest value is at the ‘rear’ end.
Here, we shall implement the peek function inside the priority queue. First, we shall import the PriorityQueue class from the built-in queue module. Then, we have a user-defined class named PQueue which inherits the PriorityQueue class. Inside the PQueue class, we shall define a function named ‘peek’, which returns the first element of ‘queue’.
from queue import PriorityQueue class PQueue(PriorityQueue): def peek(self): return self.queue myQueue = PQueue() myQueue.put(12) myQueue.put(2) myQueue.put(1) myQueue.put(7) print(myQueue.peek())
Then, we create an object of the PQueue class named ‘myQueue’. The PriorityQueue class has a function named put() to add elements to the queue. By accessing the put() function using myQueue, we shall add elements to the queue. Then we shall call the peek() function. Since it is a priority queue, the element with the lowest value will be pointed by ‘front’.
The output will be:
That sums up queue peek in python. If you have any questions in your mind, feel free to leave them in the comments below.
Until next time, Keep Learning!