Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Basic Stack Structure and Operations.
- // AUTHOR : fueanta
- // DATE : 19 Oct 2016
- #define MAX 10
- #include "iostream"
- #include <string>
- using namespace std;
- template <class T>
- class myStack {
- int top;
- T store[MAX];
- public:
- void stack_initialize();
- void push(T);
- void pop();
- T get_top();
- void show();
- bool isFull() { return top >= MAX - 1; }
- bool isEmpty() { return top < 0; }
- };
- template <typename T>
- void myStack<T>::stack_initialize() {
- top = -1;
- }
- template <typename T>
- void myStack<T>::push(T X) {
- if (this->isFull()) {
- cout << "\nOverFlow Error : Your stack is full.\n";
- }
- else {
- top++;
- store[top] = X;
- cout << "\nPush done..!" << endl;
- }
- }
- template <typename T>
- void myStack<T>::pop() {
- if (this->isEmpty()) {
- cout << "\nUnderFlow Error : Your stack is empty.\n";
- }
- else {
- top--;
- cout << "\nPop done..!" << endl;
- }
- }
- template <typename T>
- T myStack<T>::get_top() {
- return store[top];
- }
- template <typename T>
- void myStack<T>::show() {
- for (int i = top; i >= 0; i--) {
- cout << "|" << store[i] << "|" << endl;
- }
- }
- int main() {
- myStack<string> s1; int A; bool flag = true; string x;
- s1.stack_initialize();
- while (flag) {
- cout << "\nPush(1) / Pop(2) / Get top(3) / Show all(4) / End(5+)? : ";
- cin >> A;
- switch (A) {
- case 1: cout << "\nPush what? : ";
- cin >> x;
- s1.push(x);
- break;
- case 2: s1.pop();
- break;
- case 3: cout << "\nTop is : " << s1.get_top() << endl;
- break;
- case 4: s1.show();
- break;
- default: cout << "\nEnd of the Stack Machine." << endl;
- flag = false;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement