stack
stack
#include <iostream>
using namespace std;
class Stack {
private:
int stack[10];
int top;
public:
Stack() {
top = -1;
}
bool isFull() {
return top == 10 - 1;
}
bool isEmpty() {
return top == -1;
}
void pop() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
} else {
cout << "Popped " << stack[top--] << " from stack." << endl;
}
}
void display() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack: ";
for (int i = 0; i <= top; i++) {
cout << stack[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack stack;
int choice, value;
do {
cout << "What do you want to do?" << endl;
cout << "1. Push element in the stack" << endl;
cout << "2. Pop element from the stack" << endl;
cout << "3. Display the stack" << endl;
cout << "4. Exit" << endl;
switch (choice) {
case 1:
cout << "Enter the value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
cout << "Exiting" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);
return 0;
}