0% found this document useful (0 votes)
5 views3 pages

Document 3

This document contains a C++ implementation of a stack data structure with basic operations: push, pop, and display. It defines a maximum stack size and handles overflow and underflow conditions. The main function provides a menu-driven interface for users to interact with the stack.

Uploaded by

tamamshud05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Document 3

This document contains a C++ implementation of a stack data structure with basic operations: push, pop, and display. It defines a maximum stack size and handles overflow and underflow conditions. The main function provides a menu-driven interface for users to interact with the stack.

Uploaded by

tamamshud05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;

#define MAX 5

int stack[MAX], top = -1;

void push(int val) {

if (top == MAX - 1)

cout << "Stack Overflow!" << endl;

else {

stack[++top] = val;

cout << "Pushed " << val << " to stack." << endl;

void pop() {

if (top == -1)

cout << "Stack Underflow!" << endl;

else

cout << "Popped " << stack[top--] << " from stack." << endl;

void display() {

if (top == -1)

cout << "Stack is empty." << endl;

else {

cout << "Stack elements:" << endl;

for (int i = top; i >= 0; i--)


cout << stack[i] << " ";

cout << endl;

int main() {

int choice, val;

do {

cout << "\n1) Push\n2) Pop\n3) Display\n4) Exit\nEnter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter value to push: ";

cin >> val;

push(val);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

cout << "Exiting..." << endl;

break;

default:

cout << "Invalid choice!" << endl;

}
} while (choice != 4);

return 0;

You might also like