Stack [w/ Array]

#include <iostream> using namespace std; int size; int Top = -1; int stk[1000]; bool isEmpty() { return Top == -1 ? true : false; } bool isFull() { return Top == size - 1 ? true : false; } void top() { if (isEmpty()) cout << "Stack is Empty" << endl; else cout << "TOP: " << stk[Top] << endl; } void pop() { if (isEmpty()) cout << "Stack is Already Empty" << endl; else { Top--; cout << "Success" << endl; } } void push(int x) { if (isFull()) cout << "Overflow" << endl; else { stk[++Top] = x; cout << "Success" << endl; } } void show() { if (isEmpty()) { cout << "Stack is Empty" << endl; return; } for (int i = 0; i <= Top; i++) cout << stk[i] << " "; cout << endl; } int main(){ int choice, value; cout << "Enter the Maximum Size of the Stack: "; cin >> size; while (true) { cout << endl; cout << "---Press 1 - for LIST" << endl; cout << "---Press 2 - for TOP" << endl; cout << "---Press 3 - for PUSH" << endl; cout << "---Press 4 - for POP" << endl; cout << "---Press 5 - for Checking if Empty" << endl; cout << "---Press 6 - for Checking if Full" << endl; cout << "---Press 0 - Exit" << endl; cout << endl; cout << "---Enter your Choice: "; cin >> choice; cout << endl; switch (choice) { case 1: { show(); break; } case 2: { top(); break; } case 3: { cout << "Enter Value to Push: "; cin >> value; push(value); break; } case 4: { pop(); break; } case 5: { isEmpty() ? cout << "Stack is Empty" << endl : cout << "Stack is NOT Empty" << endl; break; } case 6: { isFull() ? cout << "Stack is Full" << endl : cout << "Stack is NOT Full" << endl; break; } case 0: { return 0; } default: { cout << "Invalid Input. Try Again." << endl; continue; } } } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.