Simple Stack Program in C++ Programming
Definition
A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.
Simple Stack Program
/* Simple Stack Program Example in C++*/
/* Data Structure Programs,Stack Programs,C++ Examples */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
int main() {
int item, choice, i;
int arr_stack[MAX_SIZE];
int top = 0;
int exit = 1;
cout << "\nSimple Stack Example - Array - C++";
do {
cout << "\n\nnStack Main Menu";
cout << "\n1.Push \n2.Pop \n3.Display \nOthers to exit";
cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
if (top == MAX_SIZE)
cout << "\n## Stack is Full!";
else {
cout << "\nEnter The Value to be pushed : ";
cin>>item;
cout << "\n## Position : " << top << ", Pushed Value :" << item;
arr_stack[top++] = item;
}
break;
case 2:
if (top == 0)
cout << "\n## Stack is Empty!";
else {
--top;
cout << "\n## Position : " << top << ", Popped Value :" << arr_stack[top];
}
break;
case 3:
cout << "\n## Stack Size : " << top;
for (i = (top - 1); i >= 0; i--)
cout << "\n## Position : " << i << ", Value :" << arr_stack[i];
break;
default:
exit = 0;
break;
}
} while (exit);
return 0;
}
Sample Output
Simple Stack Example - Array
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 34
## Position : 0 , Pushed Value : 34
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 89
## Position : 1 , Pushed Value : 89
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 900
## Position : 2 , Pushed Value : 900
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 450
## Position : 3 , Pushed Value : 450
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 789
## Position : 4 , Pushed Value : 789
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
## Stack is Full!
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 4 , Popped Value : 789
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 3 , Popped Value : 450
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 2 , Popped Value : 900
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 1 , Popped Value : 89
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 0 , Popped Value : 34
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Stack is Empty!
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 4
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Factorial Using Function Example Program In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Do While Loop Example Program In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Virtual Base Class Using C++ Programming