Instagram
youtube
Facebook
Twitter

Queues

Queues Introduction

  • The queue also contains an ordered set of data.
  • A queue is a linear data structure that stores data in a First In, First Out (FIFO) manner i.e. the item added earlier gets removed first.
  • A queue is an abstract data structure and is open at both ends.
  • Queue has three methods namely, Enqueue, dequeue, and peek.
  • Queue used in Operating systems like CPU scheduling, disk scheduling, etc as well as used in hardware interrupts.
  • Examples of queues are ticket lines, escalators, bookshelf, etc.

Queues Implementation

These are the following methods in queues implementation.

  • queue.Enqueue()
    • The queue.Enqueue() method adds an element at the rear of the queue.

 

  • queue.Dequeue()
    • The queue.Dequeue() method removes an element from the front of the queue.

 

  • queue.Front()
    • The queue.Front() method returns the front item from the queue.

 

  • queue.Rear()
    • The queue.rear() method returns the rear item from the queue.

 

  • queue.isEmpty()
    • The queue.isEmpty() method returns True if the queue is empty, else returns False.

We can implement a queue using a list and using its functions insert and pop.

class Queue:

    def __init__(self):
        self.queue = []

    def isEmpty(self):
        return True if len(self.queue) == 0 else False

    def front(self):
        return self.queue[-1]

    def rear(self):
        return self.queue[0]

    def enqueue(self, x: int):
        self.x = x
        self.queue.insert(0, x)       

    def dequeue(self):
        self.queue.pop()