top of page

Queue

Like Stack, Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO).   In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

Operations on Queue: Mainly the following four basic operations are performed on queue:

Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition. Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition. Front: Get the front item from the queue. Rear: Get the last item from the queue.


Python queue using list to implement :


class Queue:
    # __init__ function 
    def __init__(self, capacity):
        self.front = self.size = 0
        self.rear = capacity - 1
        self.Q = [None] * capacity
        self.capacity = capacity

    # Queue is full when size becomes 
    # equal to the capacity  
    def isFull(self):
        return self.size == self.capacity

        # Queue is empty when size is 0 

    def isEmpty(self):
        return self.size == 0

    def EnQueue(self, item):
        if self.isFull():
            print("Full")
            return
        self.rear = (self.rear + 1) % (self.capacity)
        self.Q[self.rear] = item
        self.size = self.size + 1
        print("% s enqueued to queue" % str(item))

    def DeQueue(self):
        if self.isEmpty():
            print("Empty")
            return

        print("% s dequeued from queue" % str(self.Q[self.front]))
        self.front = (self.front + 1) % (self.capacity)
        self.size = self.size - 1

    def que_front(self):
        if self.isEmpty():
            print("Queue is empty")

        print("Front item is", self.Q[self.front])

        # Function to get rear of queue 

    def que_rear(self):
        if self.isEmpty():
            print("Queue is empty")
        print("Rear item is", self.Q[self.rear]) 

C++ Queue:

  • empty

  • size

  • front

  • back

  • push_back

  • pop_front

It is in <queue> lib.

Recent Posts

See All

Array in c++

An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices...

Kommentare


bottom of page