Representation of a Stack using Array | C++ Solution

Representation of Stack using an array in C++

Representation of a Stack using Array – Stack is one of the most important linear data structures, that we can use in different algorithms or implement the application in various problem solving. A stack is a linear data structure, that means it can be easily implememented using an array.

You can use array to store some elements in the specific order you recieve them. Then you can use simple easy techniques to manage the data so that it can work like an Stack. Here we will go through the Representation of a Stack using Array.

Representation of a stack as an array

What is Stack in Cpp?

Stack is a data structure where you can add any kind of and store then in a LIFO (last in first out) manner. To know more about Stack in C++ STL : Stack in C++ STL

  • First, you push 2, 3, and 4 into the stack, so 4 goes on top.
  • The stack now looks like this (from bottom to top): [2, 3, 4].
  • When you pop, the top value 4 is removed — since stack works on Last In First Out (LIFO).
  • Now, the top of the stack becomes 3, which was pushed just before 4.
  • So, after popping once, 3 is the current top element of the stack.

Representation using an Array

There are some major differences between a stack and an array. like,

  • Array size is fixed, but the same isn’t applied for stack.
  • Also, in a stack you can only pop or push values only from or in the last element

Now let us focus on how we can implement a stack using an array.

For Example:

We are given a stack of elements:  12 , 08 , 21 , 33 , 18 , 40.

Representation of Stack As Array in C
Representation of Stack As Array in C
Representation of a stack as an array in C++ .

Code for Stack in C++ (using Class)

A stack is a linear data structure that works on the Last In First Out (LIFO) principle. Using a class in C++, a stack can be implemented with a fixed-size array and a top variable to track the top element’s index.

  • This class includes basic stack operations like push() to insert elements, pop() to remove the top element, peek() to view the top, and isEmpty() to check if the stack is empty.
  • This implementation is simple and efficient for cases where the maximum size is known and fixed in advance.

Code in C++:

Run

// Cpp Program for Implmentation of stack (array) using structure
#include<bits/stdc++.h>
using namespace std;

class Stack
{
    public:
    int top;
    int maxSize;
    int* array;
    Stack(int max)
    {
        top=-1;
        maxSize=max;
        array=new int[max];
    }
    int isFull()
    {
        if(top==maxSize-1)
        cout<<"Will not be able to push maxSize reached"<<endl;
        return top==maxSize-1;
    }

    int isEmpty()
    {
        if(top==-1)
        cout<<"Will not be able to pop minSize reached"<<endl;
        return top==-1;
    }
    void push(int item)
    {
        if(isFull()) return;
        array[++top]=item;
        cout<<"We have pushed "<<item<<" to stack"<<endl;
    }
    int pop()
    {
        if(isEmpty()) return INT_MIN;
        return array[top--];
    }
    int peek()
    {
        if(isEmpty()) return INT_MIN;
        return array[top];
    }
};

int main()
{
    Stack stack(10);
    stack.push(5);
    stack.push(10);
    stack.push(15);
    int flag=1;
    while(flag)
    {
        if(!stack.isEmpty())
            cout<<"We have popped "<< stack.pop()<<" from stack"<<endl;
        else
            cout<<"Can't Pop stack must be empty\n";
            
           flag=0;
    }

}

O/P

We have pushed 5 to stack                         
We have pushed 10 to stack
We have pushed 15 to stack
We have popped 15 from the stack

Time and Space Complexity for Stack Implementation

OperationTime ComplexitySpace Complexity
isEmpty()O(1)O(1)
push()O(1)O(n)(for array of size n)
pop()O(1)O(1)
peek()O(1)O(1)

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

FAQs - Stack implementation using Linked List in C++

The primary limitation is that the size of the stack must be predefined (#define MAX) and cannot grow dynamically. If the number of push operations exceeds this size, it leads to stack overflow. Additionally, resizing arrays at runtime is expensive compared to dynamic memory allocation used in linked list implementation.

To handle memory dynamically, you can replace the fixed-size array with a dynamically allocated array using new and resize it using techniques like doubling the array size when full (similar to dynamic array or vector behavior). You must also manage memory deallocation to prevent leaks.

Yes, two stacks can be implemented in a single array from opposite ends. One grows from the start, and the other from the end. This ensures maximum utilization of space until they collide. This method prevents space wastage that happens when one stack fills up while the other remains empty.

The time complexity of overflow check is O(1) because it involves a single comparison: whether top >= MAX – 1. It doesn’t depend on the number of elements in the stack, making it efficient and predictable in runtime.

In production-grade code, instead of printing messages, you should use exceptions. Throw a custom exception (e.g., StackOverflowException or StackUnderflowException) when an invalid operation is attempted. This approach follows better coding practices and allows higher-level logic to handle the error appropriately.

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java