Open In App

LIFO Principle in Stack

Last Updated : 30 Nov, 2023
Like Article

LIFO stands for “Last In, First Out“. This principle dictates that the last element added to the stack is the first one to be removed. In other words, the most recently added element is always at the top, and any operations on the stack will affect this top element.

Use of LIFO principle in Stack:

Push and Pop Operations:

  • Push: Imagine you put an item on the top of your stack of plates. This is like “pushing” an item onto the stack in computer terms.
  • Pop: When you take an item from the top of the stack, it’s like “popping” it off.

Use Cases of LIFO Operation:

  • Function Call Tracking: Stacks help computers remember what functions they’re doing. When you call a function, its details are pushed onto the stack. When the function finishes, its details are popped from the stack.
  • Undo/Redo Functionality: We have undo and redo buttons in many apps, like text editors. Stacks help with this. When you do something, it’s pushed onto the stack. Undoing means popping the last action and redoing means pushing it back on.
  • Expression Evaluation: Stacks are used for solving math problems with parentheses. When you read a math expression, you can use a stack to track what to do with numbers and symbols, making sure the last thing you put in is the first to be used.

Example of LIFO in a Stack:

Let’s look at a simple example to illustrate the LIFO principle in a stack. We’ll perform a series of push and pop operations on a stack:

  • Initially, the stack is empty.
  • We push the elements A, B, and C onto the stack in that order.
    • Stack: C, B, A
  • Now, let’s pop an element from the stack. Since the LIFO principle is in effect, we will remove C, which is the most recently added element.
    • Stack: B, A
  • Next, we push D onto the stack.
    • Stack: D, B, A
  • Another pop operation removes D from the stack.
    • Stack: B, A

As you can see, the last element added (D) was the first to be removed when we executed the pop operation. This is a clear demonstration of the LIFO principle at work.


The LIFO principle is a fundamental concept in computer science, and it plays a crucial role in how stacks operate. Understanding this principle is essential for designing and implementing algorithms and data structures that rely on stacks. Whether you’re working with function calls, expression evaluation, or any other stack-based operation, keeping the Last In, First Out principle in mind is key to success.

To know more about LIFO follow This link !!

Similar Reads

FIFO vs LIFO approach in Programming
FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last. Prerequisite - FIFO (First-In-First-Out) approach in Programming Real-life example: LIFO is an abbreviation for Last in, first out is the same as first in, last out (FILO).
2 min read
LIFO (Last-In-First-Out) approach in Programming
Prerequisites - FIFO (First-In-First-Out) approach in Programming, FIFO vs LIFO approach in Programming LIFO is an abbreviation for last in, first out. It is a method for handling data structures where the first element is processed last and the last element is processed first. Real-life example: In this example, following things are to be consider
6 min read
Inclusion Exclusion principle for Competitive Programming
What is the Inclusion-Exclusion Principle?The inclusion-exclusion principle is a combinatoric way of computing the size of multiple intersecting sets or the probability of complex overlapping events. Generalised Inclusion-Exclusion over Set:For 2 Intersecting Set A and B: [Tex]A\bigcup B= A + B - A\bigcap B [/Tex]For 3 Intersecting Set A, B and C:
5 min read
FIFO Principle of Queue
FIFO stands for "First In, First Out". This principle dictates that the first element added to the queue is the first one to be removed. Understanding the FIFO principle is crucial for anyone working in computer science, especially when dealing with queues. It ensures that the first task or item you add to the queue is the first to be processed or
2 min read
Pigeonhole Principle for CP | Identification, Approach & Problems
In competitive programming, where people solve tough problems with computer code, the Pigeonhole Principle is like a secret tool. Even though it's a simple idea, it helps programmers tackle complex challenges. This article is your guide to understanding how this principle works and why it's crucial for competitive programmers. Let's explore this po
8 min read
Inclusion Exclusion principle and programming applications
Sum Rule - If a task can be done in one of n1 ways or one of n2 ways, where none of the set of n1 ways is the same as any of the set of n2 ways, then there are n1 + n2 ways to do the task. The sum-rule mentioned above states that if there are multiple sets of ways of doing a task, there shouldn’t be any way that is common between two sets of ways b
13 min read
Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
Conversion of infix to postfix expression can be done elegantly using two precedence function. Each operator is assigned a value (larger value means higher precedence) which depends upon whether the operator is inside or outside the stack. Also the right and left associativity for different operators can be handled by varying it's values in the two
12 min read
Find maximum in stack in O(1) without using additional stack
The task is to design a stack which can get the maximum value in the stack in O(1) time without using an additional stack. Examples: Input: push(2) findMax() push(6) findMax() pop() findMax() Output: 2 inserted in stack Maximum value in the stack: 2 6 inserted in stack Maximum value in the stack: 6 Element popped Maximum value in the stack: 2 Appro
11 min read
Stack Permutations (Check if an array is stack permutation of other)
A stack permutation is a permutation of objects in the given input queue which is done by transferring elements from the input queue to the output queue with the help of a stack and the built-in push and pop functions. The rules are: Only dequeue from the input queue.Use inbuilt push, and pop functions in the single stack.Stack and input queue must
17 min read
Reversing a Stack with the help of another empty Stack
Given a Stack consisting of N elements, the task is to reverse the Stack using an extra stack. Examples: Input: stack = {1, 2, 3, 4, 5} Output: 1 2 3 4 5 Explanation: Input Stack: 5 4 3 2 1 Reversed Stack: 1 2 3 4 5 Input: stack = {1, 3, 5, 4, 2} Output: 1 3 5 4 2 Approach 1: Follow the steps below to solve the problem: Initialize an empty stack.Po
8 min read
Article Tags :
Practice Tags :