In Python, The `queue` is a data structure that follows the FIFO (First In First Out) Approach and is used in programming for sorting. `queue` is one of the earliest data structures defined in computer science. we can implement a queue data structure using various approaches.
One common way is to use the deque class from the collections module, which provides efficient appending and popping from both ends of the queue.
Alternatively, we can use a list and implement the queue operations manually.
Here's an example of implementing a queue using deque:
# import deque from collections module from collections import deque # Declaring Custom Queue class Queue: # constructor automatically invoked once, queue instance created. def __init__(self): self.queue = deque() # enqueue function to add item in the end of queue. def enqueue(self, item): self.queue.append(item) # queue function to get current size of queue. def size(self): return len(self.queue) def peek(self): if len(self.queue) == 0: return None return self.queue[0] # queue instance created q = Queue()
Adding Items in queue using `enqueue()` method and checking size of queue using `size()` method
q.enqueue(1) q.enqueue(2) q.enqueue(3) # checking queue size print("Queue size:", q.size()) # Output: 3
Checking Peek value in Queue else return None Data Type value
# print peek value in queue else peek function will return None Data Type value print("Front of the queue:", q.peek()) # Output: 1
Define dequeue method inside Queue Class. dequeue() removes and returns the first item from the list (the leftmost element), simulating the behaviour of a queue.
def dequeue(self): if len(self.queue) == 0: return None return self.queue.popleft()
we need to define the `is_empty()` method in the Queue class, to validate that the queue is not empty before iterating.
After creating an instance of the queue, we can print queue items using the `while` keyword.
# queue function to check is queue empty. def is_empty(self): return len(self.queue) == 0 # After creating an instance of the queue, checking if queue is not empty then, print its items. while not q.is_empty(): # Printing values of queue. print(q.dequeue()) # Output: 1, 2, 3
# import deque from collections module from collections import deque # Declaring Custom Queue class Queue: # constructor automatically invoked once, queue instance created. def __init__(self): self.queue = deque() # enqueue function to add item in the end of queue. def enqueue(self, item): self.queue.append(item) def dequeue(self): if len(self.queue) == 0: return None return self.queue.popleft() # queue function to get current size of queue. def size(self): return len(self.queue) # queue function to check is queue empty. def is_empty(self): return len(self.queue) == 0 def peek(self): if len(self.queue) == 0: return None return self.queue[0] # queue instance created q = Queue() # adding items within queue by calling enqueue function of Queue Class. q.enqueue(1) q.enqueue(2) q.enqueue(3) # checking queue size print("Queue size:", q.size()) # Output: 3 # print peek value in queue else peek function will return None Data Type value print("Front of the queue:", q.peek()) # Output: 1 print("Dequeueing elements:") # Checking if queue is not empty then, print its items. while not q.is_empty(): # Printing values of queue. print(q.dequeue()) # Output: 1, 2, 3
"enqueue(item)" adds an item to the end of the list.
"dequeue()" removes and returns the first item from the list (the leftmost element), simulating the behaviour of a queue.
"size()" returns the current size of the queue.
"is_empty()" checks if the queue is empty.
"peek()" checks the peek value in a queue using the "peek" method.