If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.
In this post, we will see how to implement Stack using Array in java.
Introduction
Stack
is abstract data type which demonstrates Last in first out (LIFO) behavior
. We will implement same behavior using Array.
Although java provides implementation for all abstract data types such as Stack
,Queue
and LinkedList
but it is always good idea to understand basic data structures and implement them yourself.
Please note that Array implementation of Stack is not dynamic in nature. You can implement Stack through linked list for dynamic behavior.
Stack basic operations
Stack supports following basic operations.
push
: Push element to the top of the Stack.This operation will increase size of stack by 1.
pop
: Remove element from the top of the Stack and returns the deleleted Object.This operation will decrease size of stack by 1.
isEmpty
: Check if Stack is empty or not.
isFull
: Check if Stack is full or not.
peek
: Returns top element from the stack without removing it.
O(1)
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 |
package org.arpit.java2blog; /** * @author Arpit Mandliya */ public class StackCustom { int size; int arr[]; int top; StackCustom(int size) { this.size = size; this.arr = new int[size]; this.top = -1; } public void push(int pushedElement) { if (!isFull()) { top++; arr[top] = pushedElement; System.out.println("Pushed element:" + pushedElement); } else { System.out.println("Stack is full !"); } } public int pop() { if (!isEmpty()) { int returnedTop = top; top--; System.out.println("Popped element :" + arr[returnedTop]); return arr[returnedTop]; } else { System.out.println("Stack is empty !"); return -1; } } public int peek() { if(!this.isEmpty()) return arr[top]; else { System.out.println("Stack is Empty"); return -1; } } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (size - 1 == top); } public static void main(String[] args) { StackCustom StackCustom = new StackCustom(10); StackCustom.pop(); System.out.println("================="); StackCustom.push(10); StackCustom.push(30); StackCustom.push(50); StackCustom.push(40); System.out.println("================="); StackCustom.pop(); StackCustom.pop(); StackCustom.pop(); System.out.println("================="); } } |
=================
Pushed element:10
Pushed element:30
Pushed element:50
Pushed element:40
=================
Popped element :40
Popped element :50
Popped element :30
=================
As you can see we have pushed 40 in last, so it is popped first as Stack is of Last In First Out(LIFO)
nature.
Conclusion
You have learnt about Stack, its basic operations and stack implementation in java using array.
That’s all about Stack implementation in java.