Growable array based stack
Last Updated :
29 Jul, 2022
We all know about Stacks also known as Last-In-First-Out(LIFO) structures. Stack primarily has two main operation namely push and pop, where push inserts an element at top and pop removes an element from top of the stack.
Now, whenever an implementation of stack is considered its size is pre-determined or fixed. Even though it is dynamically allocated, still once it is made its size cannot be changed. And hence a condition called "stack full" arises.

But what if a stack can grow as more elements are inserted or more elements are going to be inserted in future. Remember, we are talking about array based Stack. Growable Stack is the concept of allocating more memory such that "stack full" condition does not arises easily.
A Growable array-based Stack can be implemented by allocating new memory larger than previous stack memory and copying elements from old stack to new stack. And then at last change the name of new stack to the name which was given to old stack
There are two strategy for growable stack:
- Tight Strategy : Add a constant amount to the old stack (N+c)
- Growth Strategy : Double the size of old stack (2N)

There are two operation on growable stack:
- Regular Push Operation: Add one element at top of stack
- Special Push Operation: Create a new stack of size greater than old stack (according to one of the strategy above) and copy all elements from old stack and then push the new element to the new stack.
Implementation:
C++
// CPP Program to implement growable array based stack
// using tight strategy
#include <iostream>
using namespace std;
// constant amount at which stack is increased
#define BOUND 4
// top of the stack
int top = -1;
// length of stack
int length = 0;
// function to create new stack
int* create_new(int* a)
{
// allocate memory for new stack
int* new_a = new int[length + BOUND];
// copying the content of old stack
for (int i = 0; i < length; i++)
new_a[i] = a[i];
// re-sizing the length
length += BOUND;
return new_a;
}
// function to push new element
int* push(int* a, int element)
{
// if stack is full, create new one
if (top == length - 1)
a = create_new(a);
// insert element at top of the stack
a[++top] = element;
return a;
}
// function to pop an element
void pop(int* a)
{
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
top--;
}
// function to display
void display(int* a)
{
// if top is less than 0, that means stack is empty
if (top < 0)
cout << "Stack is Empty" << endl;
else {
cout << "Stack: ";
for (int i = 0; i <= top; i++)
cout << a[i] << " ";
cout << endl;
}
}
// Driver Code
int main()
{
// creating initial stack
int* a = create_new(a);
// pushing element to top of stack
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
// pushing more element when stack is full
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
// pushing more element so that stack can grow
a = push(a, 9);
display(a);
return 0;
}
Java
// Java Program to implement growable array based stack
// using tight strategy
class GFG {
// constant amount at which stack is increased
static final int BOUND = 4;
// top of the stack
static int top = -1;
// length of stack
static int length = 0;
// function to create new stack
static int[] create_new(int[] a)
{
// allocate memory for new stack
int[] new_a = new int[length + BOUND];
// copying the content of old stack
for (int i = 0; i < length; i++)
new_a[i] = a[i];
// re-sizing the length
length += BOUND;
return new_a;
}
// function to push new element
static int[] push(int[] a, int element)
{
// if stack is full, create new one
if (top == length - 1)
a = create_new(a);
// insert element at top of the stack
a[++top] = element;
return a;
}
// function to pop an element
static void pop(int[] a)
{
if (top < 0) {
System.out.println("Stack is Empty");
return;
}
top--;
}
// function to display
static void display(int[] a)
{
// if top is less than 0, that means stack is empty
if (top < 0)
System.out.println("Stack is Empty");
else {
System.out.print("Stack: ");
for (int i = 0; i <= top; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
// Driver Code
public static void main(String args[])
{
// creating initial stack
int[] a = create_new(new int[length + BOUND]);
// pushing element to top of stack
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
// pushing more element when stack is full
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
// pushing more element so that stack can grow
a = push(a, 9);
display(a);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 Program to implement growable array based stack
# using tight strategy
# constant amount at which stack is increased
BOUND = 4
# top of the stack
top = -1
a = []
# length of stack
length = 0
# function to create new stack
def create_new():
global length
# allocate memory for new stack
new_a = [0 for i in range(length + BOUND)]
# copying the content of old stack
for i in range(length):
new_a[i] = a[i]
# re-sizing the length
length += BOUND
return new_a
# function to push new element
def push(element):
global top, a
# if stack is full, create new one
if (top == length - 1):
a = create_new()
top += 1
# insert element at top of the stack
a[top] = element
return a
# function to pop an element
def pop():
global top
# stack is empty can't pop
if (top < 0)
print("Stack is Empty")
else:
top -= 1
# function to display
def display():
global top
# if top is less than 0, that means stack is empty
if (top < 0)
print("Stack is Empty")
else:
print("Stack: ", end='')
for i in range(top + 1):
print(a[i], end=' ')
print()
# Driver Code
if __name__ == '__main__':
# creating initial stack
a = create_new()
# pushing element to top of stack
push(1)
push(2)
push(3)
push(4)
display()
# pushing more element when stack is full
push(5)
push(6)
display()
push(7)
push(8)
display()
# pushing more element so that stack can grow
push(9)
display()
# This code is contributed by rutvik_56.
C#
// C# Program to implement growable array based stack
// using tight strategy
using System;
class GFG {
// constant amount at which stack is increased
static int BOUND = 4;
// top of the stack
static int top = -1;
// length of stack
static int length = 0;
// function to create new stack
static int[] create_new(int[] a)
{
// allocate memory for new stack
int[] new_a = new int[length + BOUND];
// copying the content of old stack
for (int i = 0; i < length; i++)
new_a[i] = a[i];
// re-sizing the length
length += BOUND;
return new_a;
}
// function to push new element
static int[] push(int[] a, int element)
{
// if stack is full, create new one
if (top == length - 1)
a = create_new(a);
// insert element at top of the stack
a[++top] = element;
return a;
}
// function to pop an element
static void pop(int[] a)
{
if (top < 0) {
Console.WriteLine("Stack is Empty");
return;
}
top--;
}
// function to display
static void display(int[] a)
{
// if top is less than 0, that means stack is empty
if (top < 0)
Console.WriteLine("Stack is Empty");
else {
Console.Write("Stack: ");
for (int i = 0; i <= top; i++)
Console.Write(a[i] + " ");
Console.WriteLine();
}
}
// Driver Code
public static void Main(String[] args)
{
// creating initial stack
int[] a = create_new(new int[length + BOUND]);
// pushing element to top of stack
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
// pushing more element when stack is full
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
// pushing more element so that stack can grow
a = push(a, 9);
display(a);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript Program to implement growable array based stack
// using tight strategy
// constant amount at which stack is increased
let BOUND = 4;
// s_top of the stack
let s_top = -1;
// length of stack
let length = 0;
// function to create new stack
function create_new(a) {
// allocate memory for new stack
let new_a = new Array(length + BOUND);
// copying the content of old stack
for (let i = 0; i < length; i++)
new_a[i] = a[i];
// re-sizing the length
length += BOUND;
return new_a;
}
// function to push new element
function push(a, element) {
// if stack is full, create new one
if (s_top == length - 1)
a = create_new(a);
// insert element at s_top of the stack
a[++s_top] = element;
return a;
}
// function to pop an element
function pop(a) {
if (s_top <0)
document.write("Stack is Empty" + "<br>");
else
s_top--;
}
// function to display
function display(a) {
// if s_top is less than 0, that means stack is empty
if (s_top <0)
document.write("Stack is Empty" + "<br>");
else {
document.write("Stack: ");
for (let i = 0; i <= s_top; i++)
document.write(a[i] + " ");
document.write("<br>");
}
}
// Driver Code
// creating initial stack
let a = create_new(new Array(length + BOUND));
// pushing element to s_top of stack
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
// pushing more element when stack is full
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
// pushing more element so that stack can grow
a = push(a, 9);
display(a);
// This code is contributed by _saurabh_jaiswal
</script>
OutputStack: 1 2 3 4
Stack: 1 2 3 4 5 6
Stack: 1 2 3 4 5 6 7 8
Stack: 1 2 3 4 5 6 7 8 9
Complexity Analysis:
- Time Complexity: O(n)
- Space Complexity: O(n)
Similar Reads
How to create mergeable stack? Design a stack with the following operations. push(Stack s, x): Adds an item x to stack s pop(Stack s): Removes the top item from stack s merge(Stack s1, Stack s2): Merge contents of s2 into s1. Time Complexity of all above operations should be O(1). If we use array implementation of the stack, then
8 min read
Difference between Stack and Array Stack: A stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, called the top. A stack follows the LIFO (Last In First Out) principle, i.e., the element inserted at the last is the first element to come out. The insertion of an element into a
3 min read
Reverse an array using Stack Given an array arr[] of size n, the task is to reverse the array using Stack.Examples:Input: arr = [10, 20, 30, 40, 50] Output: 50 40 30 20 10 Explanation: Upon reversing the array becomes [50, 40, 30, 20, 10]. Therefore, the output is 50 40 30 20 10.Input: arr = [ 1 ]Output: 1Explanation: Reversing
4 min read
Stack Java Library In Java, there are multiple ways to use built-in libraries for Stack.Stack Class It is a legacy collection from early Java versions. It is outdated and rarely used in modern JavaIt's synchronized and thread-safe, which can be slower in single-threaded applications like doing data structures and CP p
2 min read
Sorting array using Stacks Given an array of elements, the task is to sort these elements using a stack. Prerequisites: Stacks Examples: Input : 8 5 7 1 9 12 10 Output : 1 5 7 8 9 10 12 Explanation : Output is sorted element set Input : 7 4 10 20 2 5 9 1 Output : 1 2 4 5 7 9 10 20 We basically use Sort a stack using a tempora
6 min read