Implementation of Stack using Array

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
By Jitender Punia
Free
star4.9
Enrolled: 1000
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
Jitender Punia
Free
4.9
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Modern programming languages use data structures for faster and easier implementation of today’s modern software, one of which is a Stack. Stacks are widely used in numerous places, like Undo Functionality, Browser History, Back Button, etc. A Stack is a linear data structure that supports LIFO functionality, i.e., Last in First Out. According to LIFO, the element inserted last will be removed first, and similarly, the element inserted first will be removed last.

Scope

In this article we will discuss the following things:

  • What is a Stack?
  • Implementation of stack using the array.
  • What are the operations performed using a stack?
  • Applications of a stack
  • Finally, we will discuss the pros and cons of stack implementation using the array.

Introduction

An array of one dimension can be used to build a stack data structure. However, a stack that is constructed using an array only stores a set number of data values. It's really easy to put this into practice. Simply build a one-dimensional array of a certain size, then add or remove values from that array using the LIFO principle with the use of a variable called "top". The top is initially set at -1. Every time we wish to add a value to the stack, we first add one to the top value before inserting it. When a value has to be removed from the stack, the top value is removed, and the top value is reduced by one.

Stack Implementation using an Array

Stack is one of the most commonly used data structures in today's programming world. The major operations of the stack are as follows.

  • push(value)
  • pop()
  • isEmpty()
  • isFull()
  • Peek()

Let's discuss each of these operations in details:

push(value)

This operation is used to insert values into the stack. It inserts elements on top of the stack.

Algorithm

  • Check if the top is equal to the size of the array. If true, print "Stack is Full" and return.
  • If false, increment the top by one.
  • Assign an item to the top of the stack.

Code

C++:

Java:

Python:

pop()

This operation is used to remove an element from the stack. It uses the LIFO property to remove elements from the stack, i.e., the element inserted last will be removed first from the stack. It returns the removed element from the stack.

Algorithm

  • Check if the top is equal to -1. If true, then print "Stack is Empty" and return.
  • If false, then store the top item in a variable.
  • Decrement the top.
  • Return the stored item through the variable.

Code

C++:

Java:

Python

isEmpty()

This operation is used to check whether the stack is empty or not. It returns a boolean variable, True or False, indicating whether the stack is empty or has some elements in it.

Algorithm:

  • Check if the top is equal to -1. If True, returns True.
  • Else return false.

Code:

C++:

Java:

Python:

isFull()

This operation is used to check whether the stack is full or not. It returns a boolean variable, True or False, indicating whether the stack is full or some more elements can still be inserted.

Algorithm

  • Check if the top is equal to the size of the array, i.e., n, then return True.
  • Otherwise, return False.

Code:

C++:

Java:

Python:

Peek()

This operation is used to see the top element of the stack. The Peek function will return the top element without removing it. It will work only when the stack is not empty.

Algorithm:

  • Check if the stack top is equal to -1. If true, then print "Stack is Empty" and return.
  • If false, store the top of the stack item in a variable.
  • Return that variable.

Code:

C++:

Java:

Python:

Pros and Cons of a Stack Implementation using an Array

Pros

  • The pointers in an array-based stack implementation can be stored without using any additional memory.
  • It is more time-efficient than the stack implementation utilizing linked lists.
  • The implementation of the stack using an array is more secure and reliable.
  • Array implementation of Stack is highly recommended in allocation and deallocation.

Cons

  • Because the stack's size is constant, an array cannot be used to increase or decrease the stack's size.
  • Given that the items are stored in sequential memory locations, insertion and deletion in an array are fairly challenging.
  • There is a high possibility of Stack Overflow.
  • While using an Array, random access to stack positions is not possible.

Conclusion

Let's summarise our topic, the implementation of stack using an array by mentioning some of the important points.

  • A Stack is a linear data structure that uses LIFO functionality.
  • A stack can be implemented using an array and supports functions like push, pop, peek, empty and full.
  • The push and pop functions are used to insert and delete elements in the stack, respectively.
  • In the stack implementation of the array, we maintain the top with a variable and the stack has a predefined size that cannot be increased later.
  • Stack implementation using an array is much more time-efficient as compared to other methods.