1.

What is a Stack? How does it differ from a Queue?

Answer»

The stack is a linear data structure that follows First In Last Out (FILO) or Last In First Out (LIFO) order. The first element pushed to the stack is the last element to be popped out. Both PUSH() and pop() methods operate at one END of the stack i.e. Top.  Both push and pop OPERATIONS operate in constant time i.e. O(1). The stack is said to be in underflow state when empty and OVERFLOW state when full. In addition, it is a collection of homogenous data TYPES only.

A queue, on the other hand, follows First in First Out (FIFO) order i.e. an element that is enqueued or added to the queue first is the first one to be dequeued or removed. The insertion of elements happens at one end of the queue while the removal of elements happens at the other end. A queue, however, is also a linear data structure and a collection of similar data types only. The enqueue and dequeue operations run in constant time i.e. O(1).

Some of the applications of the stack are in reversing a string and top-down depth-first parsing in a Natural Language Processing task. A queue, on the other hand, can be used for breadth-first search or serving requests on a shared resource like printer etc.



Discussion

No Comment Found