Explain principles of FIFO (queue) and LIFO (stack) in data structures

FIFO and LIFO are commonly heard terms when discussing many different fields. It is also a popularly used method in computing and accounting. Meaning of this abbreviations are, respectively: first in, first out and last in, first out. LILO (Last In, Last Out) is synonymous with FIFO and FILO (First In, Last Out) is synonymous with LIFO.

First In, First Out (Queue)

FIFO is a term that refers to use whatever was added first. For example, let’s assume that a stack of trays is being set on the floor. The older trays are stacked on the bottom while the newer trays are stacked on the top. Now, when we work using FIFO, the last tray which arrived first would be used first. The trays will be used in the same way the trays were stacked.

The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.

Source: en.wikipedia.org

Last in, First Out (Stack)

Last In, First Out (LIFO) is a term that is used to refer to whatever was added last is used first. Using the tray example; the trays are stacked in order of the oldest one at the end and the newest one at the top. Now, newer trays are added to the top and they are used in the same way. The newer trays are used first, with the older trays being used last.

In data structures, concept is the same: let's assume we have an array, but we can add new elements only on top of it and pop elements only from top of it. It would behave as stack of trays from example above. In fact, such array is called stack on computer programming.

Stack

Source: everythingcomputerscience.com

Short answer

  • Stack data structure uses Last in, First Out principle, which means the newer elements are used first, with the older ones being used last.
  • Queue data structure uses First In, First Out principle, which means the last element which arrived first would be used first.

Similar articles

Comments