stack

 avatar
quoc14
c_cpp
5 months ago
675 B
2
Indexable
caidat
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;

int stack[10111];
int cur = 0;

void push(int x) {
    cur++;
    stack[cur] = x;
}

void pop() {
    
    stack[cur] = -1;
    cur--;
}

bool isEmpty() {
    if (cur == 0) {
        return true;
    }
    return false;
}

void show() {
    
    for (int i = 1; i <= cur; i++) {
        
        cout << stack[i] << " ";
    }
    cout << endl;
}

int main() {
    // Write C++ code here
    push(1);
    push(2);
    push(3);
    show();
    
    pop();
    show();
    
    while (!isEmpty()) {
        
        pop();
    }
    show();
    push(5);
    show();
    
    return 0;
}
Leave a Comment