Cycle 02
Lab Cycle 2 — Stack & Queue
Stack using Array
Problem Statement
Write a program to implement stack operations (push, pop, display) using an array.
#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