Code
// stack push and pop#include <iostream>using namespace std;int main(){int stack[10], top=-1, choice, n, i;cout << "Enter the size of stack: ";cin >> n;do{cout << "1. Push" << endl;cout << "2. Pop" << endl;cout << "3. Display" << endl;cout << "4. Exit" << endl;cout << "Enter your choice: ";cin >> choice;if(choice == 1){if(top == n-1){cout << "Stack is full";}else{top++;cout << "Enter the element to push: ";cin >> stack[top];}}else if(choice == 2){if(top == -1){cout << "Stack is empty";}else{cout << "The popped element is: " << stack[top];top--;}}else if(choice == 3){if(top == -1){cout << "Stack is empty";}else{cout << "The elements in the stack are: ";for(i=top; i>=0; i--){cout << stack[i] << " ";}cout << endl;}}}while(choice != 4);}
Output
run to the code buddy :)
0 Comments
Post a Comment