DEV Community

Cover image for Queue Data Structure
Aya Bouchiha
Aya Bouchiha

Posted on • Updated on

Queue Data Structure

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.

queue data structure

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Dequeue

def dequeue(self):
        if not self.isEmpty() :
            self.items.pop(0)
            self.rear -= 1 
            return 'deleted successfuly'
        return 'empty'
Enter fullscreen mode Exit fullscreen mode

isEmpty

    def isEmpty(self) -> bool:
        return  self.rear == self.front
Enter fullscreen mode Exit fullscreen mode

isFull

    def isFull(self) -> bool :
        return self.size == self.rear
Enter fullscreen mode Exit fullscreen mode

References and useful ressources

#day_3
Have a good day!

Top comments (2)

Collapse
 
rzaw profile image
Matijs

Hello everyone. This is my first comment on this platform.

Could you provide some examples were this structure would be useful?

Collapse
 
ayabouchiha profile image
Aya Bouchiha

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.