# Why Stack Is Called LIFO Data Structure?

//

Scott Campbell

Why Stack Is Called LIFO Data Structure?

A stack is a fundamental data structure in computer science that follows the Last-In-First-Out (LIFO) principle. In this article, we will explore the reasons behind why a stack is referred to as a LIFO data structure.

## Understanding the Stack Data Structure

A stack is an abstract data type that represents a collection of elements. It can be visualized as a pile of objects where only the topmost element is accessible, and all other elements are hidden underneath.

The key operations performed on a stack are:

• Push: Adds an element to the top of the stack.
• Pop: Removes and returns the topmost element from the stack.
• Peek: Returns the value of the topmost element without removing it.

## The LIFO Principle

The term “LIFO” stands for “Last-In-First-Out,” which accurately describes how a stack operates. The last element inserted into the stack is always the first one to be removed.

To illustrate this principle further, let’s consider an analogy:

• You have a pile of books stacked on top of each other on your desk.
• You decide to add another book to your collection by placing it at the top of the existing stack.
• If you need to retrieve a book from this pile, you would naturally start with the one you added most recently, which happens to be at the top.
• If you continue removing books from this pile, you will gradually reach older books that were added earlier and are therefore located at the bottom of the stack.

This analogy perfectly aligns with the behavior of a stack data structure. The last book you added is the first one removed, just like how the top element of a stack is the first one to be popped off.

## Applications of LIFO Data Structure

The LIFO property of stacks makes them suitable for various real-world applications:

• Function call stack: Programming languages use a stack to manage function calls, ensuring that each function completes its execution before returning to the calling function.
• Expression evaluation: Stacks are utilized in evaluating arithmetic expressions, such as postfix notation, by pushing operands and operators onto the stack and performing operations in a specific order.
• Undo/Redo functionality: Many software applications implement undo and redo operations using stacks to store previous states or actions that can be reversed or repeated.

## In Conclusion

A stack is called a LIFO data structure because it follows the Last-In-First-Out principle. The most recently added element is always the first one to be removed. Understanding this fundamental aspect helps in comprehending how stacks are utilized in various applications within computer science and programming.