Stack

 Analogy - A teacher used to take class test on Sunday. But students were not happy with the class test and most of them usually used to come late on the test day. Teacher was not happy with their habit. So to tackle this situation he prepared a rule called "test time adjustment". It was as below:

He arranged a very long desk to allocate the seat for students. The very first student who will reach early will get 60 minute to complete the test the next one will get 59 minutes and 30 seconds. The same way the student who was coming next after next will be getting 30 seconds less time to complete the test.

For smooth handling of students he arranged seat arrangement like the first student will sit at last position of the desk and others will follow the same pattern where last student will get the first position. While submitting the copy the last appeared student( first seat holder) will submit copy first and the first student who appeared to the test will submit his copy at last. 

In technical term above data structure is referred as Stack data structure. Below figure illustrate the same.


Definition - Stack is a linear data structure which holds collection of data where the last added data to the stack is processed first and the first added data to the stack is processed last. That's why it is also called LIFO(Last In First Out) or FILO(First In Last Out) data structure.

Variables and Operations On The Stack:

TOP - It is a position variable which always points to the top element of the stack. i.e. the last pushed element or the element to be popped.

PUSH() - This is the operation to add element in the stack. Below figure shows the push operation in the stack.







POP() - This is the operation to get and remove top element from the stack. Below figure shows the push operation in the stack.



PEEK() - This operation returns the value of top element of the stack.






IsEmpty()- This operation is used to check if Stack is empty or not? If stack is empty it will return true otherwise false.

IsFull() - This operation is used to check if Stack is full or not? If stack is full it will return true otherwise false.

Undo/Redo operation in a document file is mainly implemented using stack. Please follow the below link to see the example.



Java has inbuilt class Stack to implement stack operation. Below example shows how to use Stack class provided by JAVA:

import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
       
        Stack<Integer> stack=new Stack<>();
        
        stack.push(1);//Adding/Pushing element to the stack
        stack.push(3);
        stack.push(4);
         System.out.println("===All Opertion in itertion 1===");
          if(!stack.isEmpty())
          {
               System.out.println("Peek Element-"+stack.peek());//Top element of the stack
               System.out.println("Popped Element-"+stack.pop());//Removing an element from the stack
               System.out.println("Peek Element-"+stack.peek());
               System.out.println("Is stack Empty-"+stack.isEmpty());//Check if stack is empty or not?
          }
          
          System.out.println("===All Opertion in itertion 2===");
          if(!stack.isEmpty())
          {
               System.out.println("Peek Element-"+stack.peek());
               System.out.println("Popped Element-"+stack.pop());
               System.out.println("Peek Element-"+stack.peek());
               System.out.println("Is stack Empty-"+stack.isEmpty());
          }
          
          System.out.println("===All Opertion in itertion 3===");
          if(!stack.isEmpty())
          {
               System.out.println("Peek Element-"+stack.peek());
               System.out.println("Popped Element-"+stack.pop());
               System.out.println("Is stack Empty-"+stack.isEmpty());
          }
    }
}


Previous Post Next Post