Stack Data Structure Using Arrays

In this module, we will cover

  • The core idea behind the Stack
  • Fundamental operations of a Stack 
  • Array implementation of a Stack in c++
  • Explanation of code
A stack is a list with the restriction that insertions and deletions can be performed in only one position, namely the end of the list called the top. An explanatory video from YouTube.



Stack implements LIFO( last in first out ) or FILO( first in last out ) policy.

Fundamental operations

  1. push ( equivalent to insert ): data is inserted at the top position
  2. pop ( equivalent to delete ): deletes the most recently inserted element

Let's consider the spring-loaded stacks of plates used in cafeterias, the order in which plates are popped from the stack is the reverse of the order in which they are pushed onto the stack since only the top plate is accessible.


Array implementation of  stack

Basic operations :

  1. push( x ) : insert element x at the top of a stack
  2. pop( ) : removes an element from the top of a stack
  3. topElement( ) : access the top element of a stack
  4. isEmpty( ) : check whether a stack is empty
  5. size( ) : determines the current size of a stack

Code :


Explanation of code :



Practice Problems

  1. Balanced Brackets ( HackerEarth )
  2. Stack and Queue ( HackerEarth )
  3. Minimum add to make parenthesis valid ( HackerEarth )
  4. Equal Stacks ( Hackerrank )
  5. Game of two stacks ( Hackerrank )
That's all for this article, after completing Linked List we will implement this again using a Linked list. In the next article, we will discuss the Queue.

So, for now, practice the above problems and if you get stuck then ask your questions through comment section or if you have any content related to the topics discussed above and want to add here, then you can mention in the comment section for which we will be thankful to you.

Thank You for Reading

Comments