Stack Class Code

 avatarkaziamir
c_cpp
2 months ago
1.1 kB
12
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

#define size 10

int top = -1;
int myStack[size];

int isEmpty(){

    if(top==-1){
        return 1;
    }
    else{
        return 0;
    }

}

int isFull(){
    //return top==size-1 ? 1 : 0;
    if(top==size-1){
        return 1;
    }
    else{
        return 0;
    }
}

void push(int x){
    if(isFull() != 1){
        cout<<"Pushing Element "<<x<<endl;
        top++;
        myStack[top] = x;
        
    }
    else{
        cout<<"Sorry! Stack Full. Cant PUSH"<<endl;
    }
}
void pop(){
    if(isEmpty()!=1){
        
        int toPop = myStack[top];
        myStack[top] = 0;
        top--;
        cout<<"Poping Element "<<toPop<<endl;
        toPop = 0;
    }
    else{
        cout<<"Sorry! Stack empty. Can't POP"<<endl;
    }
}

void output(){
    cout<<"Stack Now: ";
    for(int i=0;i<=top;i++){
        cout<<myStack[i]<<" ";
    }
    cout<<endl;
    cout<<"Top now: "<<top<<endl;
}
int main()
{
    push(7);
    push(9);
    output();
    pop();
    output();
    pop();
    output();
    pop();
    output();
    

    return 0;
}