Stack Data Structure
Tags: algorithms, java, computerscience, codenewbie
Published: 2021-02-21 15:57:59

### In day-today life we have encounter this stack data structure for example stack of books or stack of boxes etc...
&nbsp;
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:-
&nbsp;
![Stack data structure visualize](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86uvzehwdsxnj0ceqnyj.PNG)
&nbsp;
## Before implementing let's understand few terminologies of stack data structure:-
&nbsp;  
> 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.
&nbsp;
```
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;
    }
}
```
&nbsp;

So this will look something like the below image:-
&nbsp;
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvd17ubppdf4dajf1fhc.PNG)
&nbsp;
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.
&nbsp;
```
public void push(int value) {
        if(isFull()) {
            System.out.println("Stack is full");
        } else {
            arr[++top] = value;
        }
 }


 public boolean isFull() {
        return top == capacity - 1;
    }
```
&nbsp;
### After adding few values into the stack it will look like below image:-
&nbsp;
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1ip1x14jdnoc5n7zys9.PNG)
&nbsp;
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.
&nbsp;
```
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;
    }
```
&nbsp;
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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66tt75bsgdii0baffnud.PNG)
&nbsp;

### Peak and display method code :-
&nbsp;
```
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]);
            }
        }
    }
```
&nbsp;
### And here our driver method, most important code else nothing will work!
&nbsp;
```
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();
    }
```
&nbsp;

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.
&nbsp;