**Introduction**

Have you always wanted to learn about stacks? Do you also want to know how to implement it in code or a way to visualize it? Then, you are at the right place. This article aims to provide you with in-depth knowledge about stack implementation in one of the easiest and systematic ways.

Before discussing more implementation, let’s brush up on our knowledge about stacks.

So, What are stacks? No, I don’t want you to think of it as a coding question; just imagine how a stack looks in the practical world.

DIsks stacked

So, we have five disks stacked together, as shown above. The numbers represent the order in which we place them one by one on top of each other. So, this is what a stack looks like.

**Stack** is a linear data structure that allows the most recently added elements to be removed first and this feature is more popularly known as LIFO ( last in, first out). So, both the insertion and deletion of elements happen only from one end, i.e. from the top of the stack.

The main functions of the stack include –

- Pop – to remove the top element from the stack
- Push – to insert an element into the stack
- Peek or top – to find the top element of the stack
- isEmpty – to check if the stack is empty or not

Now that you are confident about the foundation, let’s think about how to mimic this stack in our code, i.e. how to implement it. Maybe you should wonder –

- What data structures should be used?
- How to implement the primary functions of the stack?

**Stack Implementation Using Array**

Arrays are also linear data structures that we are now going to use for stack implementation. You may think that so far, we imagine arrays as a horizontal block consisting of data elements, whereas stacks are vertical. How come we can use arrays for implementing a stack?

Let’s see this representation which helps you to visualize the array as a stack: –

So, you see that the start index of the array becomes the bottom of the stack and the ending index acts as stack top.

First, we need to declare an array with a fixed size of “MAX”. Then define a variable “*top*” and initialize it with “*-1*”.

**Functions for Stack Implementation using Arrays:**

- Push Operation – To add an element to the stack, two things we need to do

- Increment the top variable to now point to the next free slot in the array and check it does not exceed the maximum limit.
- Put the new value in that position.

PseudoCode –

if(top == MAX-1) // MAX = size of the array print ( “Stack overflow ”) else top = top+1 array[top] = val

- Pop Operation – It is used for removing elements from the stack.

Steps –

- Check if the value of top is -1, which means our stack is empty i.e.underflow condition.
- Else we will store the current data at the index “top” in a variable and then decrement top by one so that now it points to the new top of the stack.

PseudoCode –

if(top==-1) print( "stack underflow") else value = array[top] top=top-1

- Peek – To get the topmost element of the stack.

- Quite simply, just check if the top is equal to -1 or not. If not, get the value at the index top and return it; otherwise, the stack is empty.

PseudoCode –

if(top!=-1) return array[top] else print("Stack underflow")

- isEmpty – It is used to check if the stack is empty or not.

- Just check if the top equals -1, then the stack is empty; otherwise, it is not.

PseudoCode –

if(top==-1) return true else return false