Question 01 of 05

Stack using Array

Write a program to implement stack operations (push, pop, display) using an array.

C++
#include <iostream>
using namespace std;
#define MAX 5

int stack[MAX], top = -1;

void push(int val) {
    if (top == MAX-1) { cout << "Stack Overflow\n"; return; }
    stack[++top] = val;
}

void pop() {
    if (top == -1) { cout << "Stack Underflow\n"; return; }
    cout << "Popped: " << stack[top--] << endl;
}

void display() {
    for (int i = top; i >= 0; i--) cout << stack[i] << " ";
    cout << endl;
}

int main() {
    push(10); push(20); push(30);
    display();
    pop();
    display();
    return 0;
}
Question 1 of 5