What is Stack? Why is it known as LIFO? Write algorithm for PUSH, POP, and CHANGE operation on Stack.


Q.) What is Stack? Why is it known as LIFO? Write algorithm for PUSH, POP, and CHANGE operation on Stack.

Subject: Data Structures

A Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out). Mainly the following three basic operations are performed in the stack:

  • Push: Adds an element to the collection.
  • Pop: Removes an element from the collection.
  • Peek or Top: Returns the top element of the collection.

Why is it known as LIFO?

Stack is known as LIFO because it refers to the principle of how data is accessed, stored and retrieved. In a stack, the element that is pushed last is the one that is popped first. Hence, it follows the Last In First Out (LIFO) principle.

Algorithm for PUSH operation

The PUSH operation is responsible for inserting an element into the stack. Here is the algorithm for PUSH operation:

  1. Check if the stack is full. If the stack is full, then print an error message and exit the operation.
  2. If the stack is not full, then increment the top pointer to point to the next empty space.
  3. Add the new element in the position pointed to by the top pointer.

Algorithm for POP operation

The POP operation is responsible for removing an element from the stack. Here is the algorithm for POP operation:

  1. Check if the stack is empty. If the stack is empty, then print an error message and exit the operation.
  2. If the stack is not empty, then access the data element at which the top is pointing.
  3. Decrease the value of top by 1.
  4. Return the popped element.

Algorithm for CHANGE operation

The CHANGE operation is responsible for changing the value of an element in the stack. Here is the algorithm for CHANGE operation:

  1. Check if the stack is empty. If the stack is empty, then print an error message and exit the operation.
  2. If the stack is not empty, then identify the position of the element to be changed.
  3. Replace the element at the identified position with the new value.

Here is a table that summarizes the operations:

Operation Algorithm
PUSH 1. Check if the stack is full.
2. If not, increment the top pointer.
3. Add the new element.
POP 1. Check if the stack is empty.
2. If not, access the data element at top.
3. Decrease the value of top by 1.
4. Return the popped element.
CHANGE 1. Check if the stack is empty.
2. If not, identify the position of the element to be changed.
3. Replace the element at the identified position with the new value.

For example, if we have a stack of integers:

Position Value
0 10
1 20
2 30
3 40
4 50

And we perform a PUSH operation with the value 60, the stack becomes:

Position Value
0 10
1 20
2 30
3 40
4 50
5 60

If we then perform a POP operation, the stack reverts back to its previous state. If we then perform a CHANGE operation at position 2 with the value 100, the stack becomes:

Position Value
0 10
1 20
2 100
3 40
4 50