Implementation of Stack using Array

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

Stacks is a key data structure in modern programming that facilitate efficient software development with Last In First Out (LIFO) principle. Stack is created using one-dimensional arrays. This method involves a 'top' variable, which tracks the latest element, ensuring that the last added item is the first to be removed. This article delves into implementing stacks with arrays, showcasing their practical use in programming.

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.