Growable array based stack
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++
#include <iostream>
using namespace std;
#define BOUND 4
int top = -1;
int length = 0;
int * create_new( int * a)
{
int * new_a = new int [length + BOUND];
for ( int i = 0; i < length; i++)
new_a[i] = a[i];
length += BOUND;
return new_a;
}
int * push( int * a, int element)
{
if (top == length - 1)
a = create_new(a);
a[++top] = element;
return a;
}
void pop( int * a)
{
if (top < 0) {
cout << "Stack is empty" << endl;
return ;
}
top--;
}
void display( int * a)
{
if (top < 0)
cout << "Stack is Empty" << endl;
else {
cout << "Stack: " ;
for ( int i = 0; i <= top; i++)
cout << a[i] << " " ;
cout << endl;
}
}
int main()
{
int * a = create_new(a);
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
a = push(a, 9);
display(a);
return 0;
}
|
Java
class GFG {
static final int BOUND = 4 ;
static int top = - 1 ;
static int length = 0 ;
static int [] create_new( int [] a)
{
int [] new_a = new int [length + BOUND];
for ( int i = 0 ; i < length; i++)
new_a[i] = a[i];
length += BOUND;
return new_a;
}
static int [] push( int [] a, int element)
{
if (top == length - 1 )
a = create_new(a);
a[++top] = element;
return a;
}
static void pop( int [] a)
{
if (top < 0 ) {
System.out.println( "Stack is Empty" );
return ;
}
top--;
}
static void display( int [] a)
{
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();
}
}
public static void main(String args[])
{
int [] a = create_new( new int [length + BOUND]);
a = push(a, 1 );
a = push(a, 2 );
a = push(a, 3 );
a = push(a, 4 );
display(a);
a = push(a, 5 );
a = push(a, 6 );
display(a);
a = push(a, 7 );
a = push(a, 8 );
display(a);
a = push(a, 9 );
display(a);
}
}
|
Python3
BOUND = 4
top = - 1
a = []
length = 0
def create_new():
global length
new_a = [ 0 for i in range (length + BOUND)]
for i in range (length):
new_a[i] = a[i]
length + = BOUND
return new_a
def push(element):
global top, a
if (top = = length - 1 ):
a = create_new()
top + = 1
a[top] = element
return a
def pop():
global top
if (top < 0 )
print ( "Stack is Empty" )
else :
top - = 1
def display():
global top
if (top < 0 )
print ( "Stack is Empty" )
else :
print ( "Stack: " , end = '')
for i in range (top + 1 ):
print (a[i], end = ' ' )
print ()
if __name__ = = '__main__' :
a = create_new()
push( 1 )
push( 2 )
push( 3 )
push( 4 )
display()
push( 5 )
push( 6 )
display()
push( 7 )
push( 8 )
display()
push( 9 )
display()
|
C#
using System;
class GFG {
static int BOUND = 4;
static int top = -1;
static int length = 0;
static int [] create_new( int [] a)
{
int [] new_a = new int [length + BOUND];
for ( int i = 0; i < length; i++)
new_a[i] = a[i];
length += BOUND;
return new_a;
}
static int [] push( int [] a, int element)
{
if (top == length - 1)
a = create_new(a);
a[++top] = element;
return a;
}
static void pop( int [] a)
{
if (top < 0) {
Console.WriteLine( "Stack is Empty" );
return ;
}
top--;
}
static void display( int [] a)
{
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();
}
}
public static void Main(String[] args)
{
int [] a = create_new( new int [length + BOUND]);
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
a = push(a, 9);
display(a);
}
}
|
Javascript
<script>
let BOUND = 4;
let s_top = -1;
let length = 0;
function create_new(a) {
let new_a = new Array(length + BOUND);
for (let i = 0; i < length; i++)
new_a[i] = a[i];
length += BOUND;
return new_a;
}
function push(a, element) {
if (s_top == length - 1)
a = create_new(a);
a[++s_top] = element;
return a;
}
function pop(a) {
if (s_top <0)
document.write( "Stack is Empty" + "<br>" );
else
s_top--;
}
function display(a) {
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>" );
}
}
let a = create_new( new Array(length + BOUND));
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
a = push(a, 9);
display(a);
</script>
|
Output
Stack: 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)
Last Updated :
29 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...