Home » Code Snippets » C/C++ Code Snipets

C++ - STACK Implementation using Array with PUSH, POP, TRAVERSE Operations



In this code snippet we will learn how to implement STACK using Array in C++ programming language with PUSH, POP, TRAVERSE and other operations like Stack initialisation, check stack is full or empty and traversing stack items to display them.
Submitted by IncludeHelp, on August 25, 2016

C++ Code Snippet - Stack Implementation using Array in C++

#include<iostream>
#define MAX 5
 
using namespace std;
 
 
int STACK[MAX],TOP;
 
//stack initialization
void initStack(){
    TOP=-1;
}
//check it is empty or not
int isEmpty(){
    if(TOP==-1)
        return 1;
    else
        return 0;
}
 
//check stack is full or not
int isFull(){
    if(TOP==MAX-1)
        return 1;
    else   
        return 0;
}
 
void push(int num){
    if(isFull()){
        cout<<"STACK is FULL."<<endl;
        return; 
    }
    ++TOP;
    STACK[TOP]=num;
    cout<<num<<" has been inserted."<<endl;
}
 
void display(){
    int i;
    if(isEmpty()){
        cout<<"STACK is EMPTY."<<endl;
        return;
    }
    for(i=TOP;i>=0;i--){
        cout<<STACK[i]<<" ";
    }
    cout<<endl;
}
 
//pop - to remove item
void pop(){
    int temp;
    if(isEmpty()){
        cout<<"STACK is EMPTY."<<endl;
        return;
    }
     
    temp=STACK[TOP];
    TOP--;
    cout<<temp<<" has been deleted."<<endl;   
}
int main(){
    int num;
    initStack();
    char ch;
    do{
            int a;
            cout<<"Chosse \n1.push\n"<<"2.pop\n"<<"3.display\n";
            cout<<"Please enter your choice: ";
            cin>>a;
            switch(a)
            {
                case 1:
                    cout<<"Enter an Integer Number: ";
                    cin>>num;
                    push(num);
                break;
                 
                case 2: 
                    pop();
                    break;
                 
                case 3: 
                    display();
                    break;
                 
                default : 
                cout<<"An Invalid Choice!!!\n";
                 
                 
            }
            cout<<"Do you want to continue ? ";
            cin>>ch;                      
            }while(ch=='Y'||ch=='y');
    return 0;
}


Output

    Chosse  
    1.push  
    2.pop
    3.display  
    Please enter your choice: 1  
    Enter an Integer Number: 10  
    10 has been inserted.  
    Do you want to continue ? y  
    Chosse  
    1.push  
    2.pop
    3.display  
    Please enter your choice: 1  
    Enter an Integer Number: 20  
    20 has been inserted.  
    Do you want to continue ? y  
    Chosse  
    1.push  
    2.pop
    3.display  
    Please enter your choice: 3  
    20 10
    Do you want to continue ? n 





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.