Which data structure uses a “First-In-First-Out” (FIFO) approach?
a) Stack
b) Queue
c) Tree
d) Hash Table
The correct answer and explanation is :
The correct answer is b) Queue.
Here’s the explanation:
A Queue is a linear data structure that follows the “First-In-First-Out” (FIFO) principle. This means the first element added to the queue is always the first one to be removed. Think of a line of people waiting – the person who arrived first is the first person to be served. Operations on a queue include “enqueue” (adding an element to the rear) and “dequeue” (removing an element from the front).
Let’s look at why the other options are incorrect:
a) A Stack is another linear data structure, but it follows the “Last-In-First-Out” (LIFO) principle. The last element added to the stack is the first one to be removed. An analogy is a stack of plates – you add new plates on top, and you typically take the top plate off first. Operations include “push” (adding to the top) and “pop” (removing from the top).
c) A Tree is a hierarchical data structure, not a linear one. It consists of nodes connected in a parent-child relationship. While specific traversal algorithms like Breadth-First Search (BFS) can process nodes in a layer-by-layer order that might resemble FIFO for traversal, the inherent structure and typical insertion/deletion methods of a tree are not defined by a simple FIFO or LIFO principle for accessing elements based on insertion time.
d) A Hash Table (also known as a hash map) is a data structure used for implementing associative arrays (key-value pairs). It uses a hash function to compute an index into an array of buckets, where the desired value is found. The order in which elements are inserted or retrieved is generally not preserved in a specific FIFO or LIFO manner; access is based on the hash of the key.
Therefore, the Queue is the data structure explicitly designed to operate based on the First-In-First-Out (FIFO) rule.