Hi, today, we're going to discuss queue :)
Definition Of Queue
A Queue is a linear data structure that stores items in a First-In/First-Out (FIFO) manner. In Queue, the element that goes in first is the element that comes out first.
Space and Time complexity
the space complexity of a queue is O(n)
add ( enqueue ) | remove (dequeue) | search | access |
---|---|---|---|
O(1) | O(1) | O(n) | O(n) |
Implementation of queue using python
class Queue:
def __init__(self,size):
self.items = []
self.front = self.rear = 0
self.size = size
Enqueue
def enqueue(self,data):
if not self.isFull() :
self.items.append(data)
self.rear += 1
return f'{self.items[-1]} added'
else:
return 'full'
Dequeue
def dequeue(self):
if not self.isEmpty() :
self.items.pop(0)
self.rear -= 1
return 'deleted successfuly'
return 'empty'
isEmpty
def isEmpty(self) -> bool:
return self.rear == self.front
isFull
def isFull(self) -> bool :
return self.size == self.rear
References and useful ressources
- https://www.geeksforgeeks.org/queue-in-python/
- https://www.geeksforgeeks.org/queue-data-structure/
- https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm
- https://www.youtube.com/watch?v=A3ZUpyrnCbM
- https://www.youtube.com/watch?v=wjI1WNcIntg
#day_3
Have a good day!
Top comments (2)
Hello everyone. This is my first comment on this platform.
Could you provide some examples were this structure would be useful?
Hi, this is a good quesion, we can use Queue in all kinds of customer service such as Call Center phone systems in banks or in big companies to hold people calling them in an order, until a service representative is free, in addition, It is used In operating systems, for controlling access to shared system resources like files, printers and disks,queue is used in routers or switches as well, however, it is used in many algorithmes like Round Robin Schedule, Breadth First Search .
Have a great day.