0% found this document useful (0 votes)
4 views

stack

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

stack

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

Your code here:

#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 push(int value) {


if (isFull()) {
cout << "Stack overflowed!!!" << endl;
} else {
stack[++top] = value;
cout << "Pushed " << value << " into stack." << endl;
}
}

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;

cout << "Enter your choice: ";


cin >> choice;

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;
}

Your whole Screenshot here: (Console Output):

You might also like