Sobes.tech
Junior

What is a queue in the context of programming?

sobes.tech AI

Answer from AI

Queue — an abstract data type that implements the FIFO (First-In, First-Out) principle, where the first added element will be the first removed.

Main operations:

  • Enqueue: Add an element to the end of the queue.
  • Dequeue: Remove an element from the beginning of the queue.
  • Peek/Front: Get the value of the element at the front of the queue without removing it.
  • IsEmpty: Check if the queue is empty.
  • Size: Get the current number of elements in the queue.

Examples of usage:

  • Managing tasks in operating systems.
  • Processing requests in web servers.
  • Graph traversal algorithms (BFS - Breadth-First Search).
  • Buffers for sequential data processing.

Implementation can be based on arrays or linked lists.

# Example implementation of a queue using a list in Python
class Queue:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.append(item) # Add to the end

    def dequeue(self):
        if not self.is_empty():
            return self.items.pop(0) # Remove from the beginning
        return None

    def peek(self):
        if not self.is_empty():
            return self.items[0]
        return None

    def size(self):
        return len(self.items)