logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

What is a Queue and How is it Implemented?


What is a Queue and How is it Implemented

A queue is an abstract data type that follows the First In, First Out (FIFO) principle. It is used to store a collection of elements where elements are inserted at the rear (enqueue) and removed from the front (dequeue). Queues are commonly used in scenarios where tasks or processes need to be executed in the order they were received.

 

Here's a basic overview of how a Queue is Implemented:

 

  1. Representation: A queue can be implemented using various underlying data structures, such as arrays or linked lists. Each element in the queue is represented by a node containing the element's data and a reference to the next node (for linked list implementation) or stored at a specific index (for array implementation).

  2. Operations:

    • Enqueue: Adds an element to the rear of the queue. In a linked list implementation, this involves creating a new node and updating the rear pointer to point to the new node. In an array implementation, the element is inserted at the end of the array, and the rear index is incremented.
    • Dequeue: Removes and returns the front element from the queue. In a linked list implementation, this involves removing the front node and updating the front pointer to point to the next node. In an array implementation, the element at the front index is returned, and the front index is incremented to the next element.
  3. Front and Rear Pointers: In both implementations, pointers (or indices) are maintained to keep track of the front and rear of the queue. These pointers are updated accordingly with enqueue and dequeue operations.

  4. Empty and Full Conditions: In some implementations, additional checks may be added to handle cases where the queue is empty or full. For example, in an array implementation, a full condition occurs when the rear index reaches the end of the array.

  5. Other Operations: Other common operations on queues include:

    • Front: Returns the front element of the queue without removing it.
    • IsEmpty: Checks if the queue is empty.
    • IsFull: Checks if the queue is full (applicable in fixed-size implementations).
    • Size: Returns the number of elements in the queue.

 

Here's a simple example of a queue implemented using a linked list in Python:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Queue:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, data):
        new_node = Node(data)
        if self.rear is None:
            self.front = self.rear = new_node
            return
        self.rear.next = new_node
        self.rear = new_node

    def dequeue(self):
        if self.front is None:
            return None
        temp = self.front
        self.front = temp.next
        if self.front is None:
            self.rear = None
        return temp.data

    def front(self):
        return self.front.data if self.front else None

    def isEmpty(self):
        return self.front is None

# Example usage
queue = Queue()
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
print("Front element:", queue.front())  # Output: 10
print("Dequeued element:", queue.dequeue())  # Output: 10
print("Front element after dequeue:", queue.front())  # Output: 20

 

 

This example demonstrates a simple queue implementation using a linked list, supporting enqueue, dequeue, front, and is Empty operations.

 

 

Thank you,

Popular Post:

Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment