Queue Data Structure

ayabouchiha

Aya Bouchiha

Posted on June 15, 2021

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!

💖 💪 🙅 🚩
ayabouchiha
Aya Bouchiha

Posted on June 15, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Queue Data Structure
codenewbie Queue Data Structure

June 15, 2021