Stack Data Structure

Dhathri Vupparapalli
3 min readNov 7, 2021

--

Welcome!! In this article, we will learn about Stack Data Structure

Generally, Data is stored in either Linear or Non-Linear Data Structure. In Linear Data Structure, Data is stored in Sequential order. Whereas in Non-Linear Data Structure data is stored Non sequentially. In Stack, Data is stored in Linear order which means the data in stacks will be stored continuously inside the memory. That means elements are connected to previous and next nodes. The elements from the stack can be removed only from one direction. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

Read this to know more about other Linear Data Structures.

working of Stacks

One real-life example of a stack is that, Consider there is a stack of plates in the cupboard. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO(Last In First Out) or FILO(First In Last Out). An example of the Stack in computing is Back/Forward stacks on browsers.

Operations in Stack

To insert and remove an element from the stack we have inbuilt methods like push and pop. In Java, to store integers inside a stack we use the below syntax

Stack<Integer> stack = new Stack<Integer>();

We have a few inbuilt methods like push() to push the element inside a stack, pop() to remove the element out of the stack, peek() to return the top element from the stack without deleting it from the stack, isEmpty to check if the stack is empty or not. Let's now implement a stack without using the inbuilt methods, to understand the data structure in a better way.

Key Takeaways:

  • The order of elements inside a stack is LIFO (Last in First Out)
  • It is a Linear Data Structure.
  • Retrieval of data can be done in only one direction.

Implementation of Stack

To implement a stack, we need a variable called top which keeps track of the top element. Initially when the stack is empty top variable is initialized to -1.

class MyStack(){

//maxSize stores maximum number of ele that a stack can store.
private int maxSize;
private int[] stackAray;
private int top;
public MyStack(int s){
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
//to push an elemrnt into stack
public void push(int j){
stackArray[++top] = j;
}
//to pop an elemrnt from stack
public void pop(){
stackArray[top--];
}
//to return top elemrnt of stack
public int peek(){
return stackArray[top];
}
//to check if stack is empty
public boolean isEmpty(){
return (top == -1);
}
//to check if stack is full
public boolean isFull(){
return (top == maxSize - 1);
}
public static void main(String[] args) {
MyStack theStack = new MyStack(10);
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(50);

while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}

The above code will produce the following result

50  40  30 20 10

Conclusion

In this article, we have seen about Stack, which is a Linear Data Structure. We have also seen the operations that can be done inside a stack. We have also implemented the stack data structure using an array without using the inbuilt methods to understand the stack much better.

Related articles:

--

--