What is Stack : A Stack is a data structure which follows LIFO pattern (Last In First Out) means last inserted element will be the first element to be removed from the stack. Here lets see how to do the stack implementation with array.
Stack operations:
- push(element) : Push operation will add element at the top of stack.
- pop() : Pop operation will remove element from the stack and return it.
- peek() : Returns the element at the top of stack without removing.
Stack implementation using Array :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
public class StackDemo { private int stkSize; private int[] stkArr; private int top=-1; // constructor to create stack public StackDemo(int size) { this.stkSize = size; this.stkArr = new int[stkSize]; } // push method to add new element at the top of the stack public void push(int element) { if(isFull()) { System.out.println(("Stack is full. Please remove elements from the stack")); } System.out.println("Adding elements in the stack: "+element); this.stkArr[++top] = element; } // This method removes element from the top of the stack. public int pop() throws Exception { if(this.isEmpty()) { throw new Exception("Can not remove element as stack is empty.Please add elements in the stack."); } int entry = this.stkArr[top--]; System.out.println("Removing elements at the top of stack: "+entry); return entry; } //This method returns element at the top of the stack without removing it. public int peek() { return stkArr[top]; } public boolean isEmpty() { return (top == -1); } // This method will return true if the stack is full otherwise false public boolean isFull() { return (top == stkSize - 1); } public static void main(String[] args) { StackDemo stack = new StackDemo(20); for(int i=1;i<=15;i++) { stack.push(i); } for(int i=1;i<=10;i++) { try { stack.pop(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("Element at the top of stack is : "+stack .peek()); } } |
OutPut :
Adding elements in the stack: 1
Adding elements in the stack: 2
Adding elements in the stack: 3
Adding elements in the stack: 4
Adding elements in the stack: 5
Adding elements in the stack: 6
Adding elements in the stack: 7
Adding elements in the stack: 8
Adding elements in the stack: 9
Adding elements in the stack: 10
Adding elements in the stack: 11
Adding elements in the stack: 12
Adding elements in the stack: 13
Adding elements in the stack: 14
Adding elements in the stack: 15
Removing elements at the top of stack: 15
Removing elements at the top of stack: 14
Removing elements at the top of stack: 13
Removing elements at the top of stack: 12
Removing elements at the top of stack: 11
Removing elements at the top of stack: 10
Removing elements at the top of stack: 9
Removing elements at the top of stack: 8
Removing elements at the top of stack: 7
Removing elements at the top of stack: 6
Element at the top of stack is : 5
Use Cases of Stack :
- If you want to reverse a word then push each letter of this word in stack and then use pop operation to remove and return letter from the stack and concatenate these letters to form a word. You will get reverse word.
- Undo operation in MSWord document uses stack by pushing all the changes in stack and last change will be at the top of stack. When you redo changes then last applied change will be returned and removed from stack and rollback last changes in the MSWord document
- Back button is there in each web browser. When you redirected from one page to other page then those pages are pushed to stack using push operation. Current page is at the top of stack and when you press back button of browser then you will be redirected in reverse order through the pages in stack from top to bottom.
- Stack is used in Depth First search(DFS)
- JVM uses stack in memory management. Stack memory is used for thread execution. Whenever a method is called then a new frame is created in the stack for that method to hold local variables and reference to other objects.
Leave a Reply