## LIFO Approach in data structureThe word LIFO stands for For example, The number of books placed one over another Another best example real-life example is the - 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 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 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: - 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 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 stack
## LIFO Approach implementation in C++ Programming language using stack
## LIFO Approach implementation in JAVA Programming language using stack
## LIFO Approach implementation in Python Programming language using stack
## LIFO Approach implementation in C# Programming language using stack
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
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
## LIFO Approach implementation in C++ Programming language using structure to implement stack
Next TopicPolish Notation in Data Structure |