Javatpoint Logo
Javatpoint Logo

LIFO Approach in data structure

The word LIFO stands for Last In First Out, in which we will enter the data elements into the data structure. Here, we will pop out the data elements which are recently added. It means that the last element will be the first to be popped out.

For example,

The number of books placed one over another

Another best example real-life example is the Tower of Hanoi. It is an interesting game and wholly based on the last in the first out principle.

  • The data structure, based on the LIFO principle, is stack.
  • In the stack data structure, we will perform the main two operations: first is push, and another is pop.
  • The data element that is pushed recently in the stack will be popped out first; similarly, vice-versa, the data element that is pushed into the stack initially will be popped out last until we pop out all the elements present before it.
  • The stack contains only one end, or we can say it is open-ended from one side only.
  • Most of the programs are based on the concept of LIFO.
  • For the evaluation of the various expressions like postfix to infix, infix to prefix, and prefix to postfix, stack plays a very important role.
  • Its main application is the implementation of recursion. Recursion is used to solve the problem more efficiently.
  • Also, the LIFO principle is used in a heap and filling the activation records of the program while executing it.
  • For the implementation of the Pushdown automata for checking the string is accepted from the machine is not, the stack data structure is used.
  • It is mainly based on the LIFO principle, in which we will push the input string one by one into the stack whenever required and similarly pop out the same amount of the strings so that we can easily count the number of alphabets present into the given string and identify that it belongs to the mentioned context-free grammar or not.

The data structure, which is based on the LIFO principle, is a stack. We mainly perform two operations on it, push and pop. Push operation is used to push the data element into the stack, and pop operation is used to pop out the data elements from the stack.

In the stack data structure, we have a pointer named as top, which points to the value present at the top of the stack. Initially, when the stack is empty, it points to the null pointer; hence this condition is known as the underflow condition. In stack we have given with the capacity of the stack, let say n. We need to insert the data values one by one into the stack until the top of the stack reaches the capacity of the stack. If it reaches the capacity, then the condition is known as overflow.

Let us understand the concept of LIFO, with the help of an example,

If we have a container of capacity 10 plates and we need to put plates one by one into it, then we can do it by using the Last in first out principle,

The plates are numbered from 1 to 10, and we are required to push all the plates into the container in their increasing order:

LIFO Approach in data structure
  • Initially, the container is empty; hence we call it an underflow condition.
  • We will put plate 1 into the container.
  • The top of the container will point to plate number 1; after it, we will push plate 2 into the container.
  • Then now, the top pointer will point to plate number 2; similarly, we will push all the plates into the container.
  • Finally, when the top of the container points to plate number 10, it will indicate the overflow condition; hence we cannot fill other plates into the container until we pop one of the plates.
  • If we want to fetch out plate number 5 from the container, we must remove the plates from the container until we reach plate number 5.
  • First, we will pop out the recently pushed plate, i.e., plate 10, from the container.
  • After it, we will pop out plate number 9 from the container.
  • After then 8, then 7, then 6, finally the top of the container will point to plate number 5.
  • We will pop out plate 5 from the container.

From the above example, we have learned the working of the last in the first out principle.

Now, let us implement the LIFO principle using a stack in the program.

Implementation of LIFO principle using stack in the program

For implementing the LIFO approach using stack, we have provided a stack with a maximum capacity limit. For stack, we have a pointer named as top, which is initialized with -1, that shows the underflow condition of the stack, which means we cannot pop out the data elements from the stack as it is in an empty state, but we can enter the data elements into it. We will enter the data elements into the stack one by one until we reach the capacity limit of the stack; once we reach the limit of the stack, i.e., top == capacity - 1, then we cannot enter more data elements into the stack, it implies the overflow condition of the stack. To enter more data elements into the stack, we need to remove the data elements from the stack by applying the pop operation.

In this way, we will implement the LIFO principle using stack.

LIFO Approach implementation in C Programming language using stack

The output of the above program

LIFO Approach in data structure

LIFO Approach implementation in C++ Programming language using stack

The output of the above program

LIFO Approach in data structure

LIFO Approach implementation in JAVA Programming language using stack

The output of the above program

LIFO Approach in data structure

LIFO Approach implementation in Python Programming language using stack

The output of the above program

LIFO Approach in data structure

LIFO Approach implementation in C# Programming language using stack

The output of the above program

Pop:
4
3
2
1
0
Element on stack top: 4
Element is found at position 3
Element not found    

LIFO Approach implementation in JavaScript Programming language using stack

The output of the above program

Pop:
4
3
2
1
0
Element on stack top: 4
Element is found at position 3
Element not found   

LIFO Approach implementation in C Programming language using structure to implement stack

The output of the above program

LIFO Approach in data structure

LIFO Approach implementation in C++ Programming language using structure to implement stack

The output of the above program

LIFO Approach in data structure





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA