In this program, we are going to share a C++ program to implement the basic stack operation. If you are c++ beginner OR want to start learning the C++ programming language, then keep your close attention in this tutorial as we are going to share a c++ program to implement the basic stack operation with the output.
Copy the below C++ program and execute it with the help of Turbo C compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include<bits/stdc++.h> using namespace std; #define MAX 1000 class Stack { int top; public: int a[MAX]; Stack() { top = -1; } bool push(int x); int pop(); bool isEmpty(); }; bool Stack::push(int x) { if (top >= MAX) { cout << "Stack Overflow"; return false; } else { a[++top] = x; return true; } } int Stack::pop() { if (top < 0) { cout << "Stack Underflow"; return 0; } else { int x = a[top--]; return x; } } bool Stack::isEmpty() { return (top < 0); } int main() { struct Stack s; s.push(10); s.push(20); s.push(30); cout << s.pop() << " Popped from stack\n"; return 0; } |
30 Popped from stack
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.