Queues
A queue is similar to a stack, except that is processes data in a different order.
Queue are display and explain horizontaly.
It's like a line of peaple at the movie theater.
The first one in line is the first one to leave and enter the theater (FIFO).
Deque / Python
In Python, queues are a buit-in data structure with its own standard
library.
Deque in python is a versatile implementation of
double-ended queue.
It allows you to effectively use it as a
stack, a queue, or a combination of both.
from collections import deque
my_queue = deque()
my_queue.append(1)
my_queue.append(2)
my_queue.append(3)
print("Queue after insertion =", my_queue)
front = my_queue.popleft()
print("Queue after deletion =", my_queue)
print("Popped left element =", front)
Create Queue
Like many other abstarct data types, it isn't implemented in many programming
languages.
Let's create a queue to see how it
works internaly.
class Queue:
def __init__(self):
self.items = []
def append(self, item):
self.items += [item]
def popleft(self):
item = self.items[0]
self.queue = self.items[1:]
return item
def read(self):
return self.items[0]
def __str__(self):
return str(self.items)
queue = Queue()
queue.append(1)
queue.append(2)
queue.append(3)
print("Queue after insertion =", queue)
popped = queue.popleft()
print("Remove from queue front =", popped)
print("Queue after deletion =", queue)
print("New front =", queue.read())