In day-today life we have encounter this stack data structure for example stack of books or stack of boxes etc...
So, the stack data structure is structured such a way that the elements in this appears one above the other and that's why the name stack.
It follows
LIFO
pattern which means Last In First Out. So the element which is added at the last, at the removal time it's removed first and so on.
We can visualize how stack data structure looks like from below image:-
Before implementing let's understand few terminologies of stack data structure:-
Push() :- Push means pushing element in a stack.
Pop() :- Pop means removing element from stack.
Top :- Top points to the top-most element in the stack and as we push or pop elements from stack we increment or decrement top pointer.
Peak() :- Peak means the first value which is top value of the stack.
Let's see how we can implement stack using array in java, but you can use any programming language of your choice:-
First we will create a class and create three properties, check code for information about these properties.
class StackExample {
private int[] arr; // used to store our stack elements
private int top; // used to know which element is at the top
private int capacity; // total size of stack
public StackExample(int size) {
arr = new int[size];
top = -1; // will initialize with -1 as initially it is not pointing to any element
capacity = size;
}
}
So this will look something like the below image:-
So initially top is not pointing to any element
and as we push elements into the stack we will increment the top
and it will point to the top most or last pushed element.
Code for push operation :-
So, here we check if the stack is full or not, as we are using array to implement stack so it can overflow
and to keep in mind that we have check for the full condition.
public void push(int value) {
if(isFull()) {
System.out.println("Stack is full");
} else {
arr[++top] = value;
}
}
public boolean isFull() {
return top == capacity - 1;
}
After adding few values into the stack it will look like below image:-
Here the top is pointing to value 50.
Let's see code for pop operation :-
As we have checked overflow condition in push operation similarly we will check underflow
operation in pop() method. So that we will not try to access empty stack.
public void pop() {
if(isEmpty()) {
System.out.println("Stack is empty");
} else {
System.out.println("Removing value: "+arr[top--]);
}
}
public boolean isEmpty() {
return top == -1;
}
Suppose we call pop method it will remove 50 from the stack and the top will be decremented
and it will now point to 40.
Peak and display method code :-
public void peakValue() {
System.out.println("Peak value is: "+arr[top]);
}
public void display() {
if(isEmpty()) {
System.out.println("Stack is empty");
} else {
System.out.println("Stack values are: ");
for(int i=top; i >= 0; i--) {
System.out.println(arr[i]);
}
}
}
And here our driver method, most important code else nothing will work!
public static void main(String[] args) {
StackExample s1 = new StackExample(5);
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.pop();
s1.display();
s1.peakValue();
}
See how easy is to implement stack using array.
There are few advantages and disadvantages of implementing stack using array but we will not cover that in this post.
Top comments (0)