LIFO Approach in data structureThe 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, 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:
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 programFor 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 stackThe output of the above program LIFO Approach implementation in C++ Programming language using stackThe output of the above program LIFO Approach implementation in JAVA Programming language using stackThe output of the above program LIFO Approach implementation in Python Programming language using stackThe output of the above program LIFO Approach implementation in C# Programming language using stackThe 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 stackThe 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 stackThe output of the above program LIFO Approach implementation in C++ Programming language using structure to implement stackThe output of the above program Next TopicPolish Notation in Data Structure |