Stack implementation in C++

This article is about stack implementation using array in C++.


Stack as an Abstract data Type

Stack is an ordered data structure to store datatypes in LIFO (Last In First Out) order. That
means the element which enters last is first to exit(processed). It’s like a tower of concentric
rings kept one over other. Of course, the last one would be picked first when we start removing
them out one by one (e.g. :Tower of Hanoi). Stack is a similar kind of data structure(Abstract
Data Type, briefly ADT) where both insertion and deletion are done on the same end, namely
top. Stack only hold similar datatypes.
The insertion process is named as Push
The deletion process is named as Pop


Operation on Stack

Type_t = any datatype

Basic operations:

Push(Type_t data): It inserts data of datatype Type_t to the stack
Type_t Pop(): Removes the topmost element from stack and returns it

Other operations:

Type_t top(): Returns topmost element
bool isEmpty(): returns true if stack is empty, else returns false
bool isFull(): returns true if stack is full, else returns false
int size(): Returns size of the stack

Example:
Let the elements inserted are
1, 2, 3, 4

StackOperations


Stack Implementation using Array In C++

Prerequisites:

  1. top variable
  2. An Array namely stack_array
    So to implement a stack with array what we need to do is to implement those operations.
    Below is the detailed code.

//declare our pre-reuisites

int stack_array[MAX_SIZE]; //globally declared array to act like stack
int top=-1; //top variavle initialized to -1 for empty stack

//implement stack operations

//1. implement isEmpty()

bool isEmpty(){
if(top==-1) //empty stack
return true;
else
return false;

}
//2. Implement isFull()
bool isFull(){
if(top==MAX_SIZE-1)
return true;
else
return false;
}

//3. Implement push operation

void push(int data){
if(isFull()){
cout<<"Stack is full\n";
return;
}
else{
stack_array[++top]=data;//first top is increased by 1 and then stack[top]=data
cout<<data<<" is pushed\n";
}

}
//4. Implement Pop operation

int pop(){
if(isEmpty()){
cout<<"Stack is empty\n";
return INT_MIN;
}
else{
return stack_array[top–]; //top is decreased after popping
}
}

//5. implement size function

int size(){
return top+1;
}

//6. implement top

int top_stack(){
if(isEmpty()){
cout<<"stack is empty\n";
return INT_MIN;
}
else
return stack_array[top];
}
//main function
int main(){

//menu for operations
//press 1 for push (with data)
//press 2 for pop()
//press 3 for top()
//press 4 for size()
//press 0 to exit()

cout<<"press 1 for push\n";
cout<<"press 2 for pop()\n";
cout<<"press 3 for top()\n";
cout<<"press 4 for size()\n";
cout<<"press 0 for exit\n";

int choice;
cout<<"press your choice\n";
cin>>choice;
while(choice){
if(choice==1){
int data;
cout<<"Enter element\n";
cin>>data;
push(data);
}
else if(choice==2){
int item=pop();
if(item==INT_MIN){}
else
cout<<"Popped element: "<<item<<endl;
}
else if(choice==3){
int item=top_stack();
if(item==INT_MIN){}
else
cout<<"Top element: "<<item<<endl;
}
else if(choice==4){
cout<<"Size is: "<<size()<<endl;
}
else
cout<<"Invalid number, try again!\n";
cout<<"press your choice\n";
cin>>choice;

}

cout<<"Exiting…\n";
return 0;

}

Output:

press 1 for push
press 2 for pop()
press 3 for top()
press 4 for size()
press 0 for exit
press your choice
1
Enter element
2
2 is pushed
press your choice
1
Enter element
5
5 is pushed
press your choice
3
Top element: 5
press your choice
4
Size is: 2
press your choice
1
Enter element
3
3 is pushed
press your choice
2
Popped element: 3
press your choice
2
Popped element: 5
press your choice
2
Popped element: 2
press your choice
2
Stack is empty
press your choice
2
Stack is empty
press your choice
0
Exiting…

That’s all about Stack implementation using array in C++.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *