A linear queue is a data structure that follows the "first-in, first-out" (FIFO) principle. It is used to store and manage a collection of elements in a linear order, much like people waiting in a line or a queue in the real world.
Linear queues are essential in various applications where elements must be processed in the order they were added.
What is a queue?
Adding an element to the back (also known as the rear or tail) of the queue.
Removing and returning the element from the front (also known as the head) of the queue.
Viewing the element at the front of the queue without removing it.
class LinearQueue: def __init__(self): self.queue = [] def enqueue(self, item): self.queue.append(item) def dequeue(self): if not self.is_empty(): return self.queue.pop(0) else: print("Queue is empty") def is_empty(self): return len(self.queue) == 0 def size(self): return len(self.queue) # Example usage: if __name__ == "__main__": queue = LinearQueue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) print("Queue:", queue.queue) print("Dequeue:", queue.dequeue()) print("Queue:", queue.queue) print("Is empty?", queue.is_empty()) print("Size:", queue.size())