Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<stdlib.h>
- #define MAX 5
- using namespace std;
- void push();
- void pop();
- void display();
- int top = -1;
- int stack_arr[MAX];
- void main() {
- int ch;
- system("CLS");
- cout << "\n\t\t" << "Stack Operation" << endl;
- cout << "\n\t\t" << "............................." << endl;
- while (1) {
- cout << "\n*************************************" << endl;
- cout << "\n1. Push" << endl;
- cout << "\n2. Pop" << endl;
- cout << "\n3. Display" << endl;
- cout << "\n4. Exit" << endl;
- cout << "\n*************************************" << endl;
- cout << "\nEnter ur choice: ";
- cin >> ch;
- switch (ch) {
- case 1:
- push();
- break;
- case 2:
- pop();
- break;
- case 3:
- display();
- break;
- case 4:
- exit(0);
- default:
- cout << "Invalid Operation" << endl;
- }
- }
- }
- void pop() {
- if (top == -1)
- cout << "\tStack underflow" << endl;
- else {
- cout << "\nPopped element is: " << stack_arr[top];
- top -= 1;
- }
- }
- void push() {
- int pitem;
- if (top == (MAX - 1))
- cout << "\tStack underflow" << endl;
- else {
- cout << "Enter the item to be pushed in stack: " << endl;https://dwu39hunq9vsh.cloudfront.net/wp-content/uploads/2018/01/24142523/Soon-Yi1.jpg
- cin >> pitem;
- top += 1;
- stack_arr[top] = pitem;
- }
- }
- void display() {
- if (top == -1)
- cout << "\nStack is empty" << endl;
- else {
- cout << "\nStack element" << endl;
- for (int x = top; x >= 0; x--) {
- cout << stack_arr[x] << "\t";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement