fueanta

myStack

Oct 19th, 2016
175
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Basic Stack Structure and Operations.
  2. // AUTHOR : fueanta
  3. // DATE : 19 Oct 2016
  4.  
  5. #define MAX 10
  6. #include "iostream"
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. template <class T>
  12. class myStack {
  13.     int top;
  14.     T store[MAX];
  15. public:
  16.     void stack_initialize();
  17.     void push(T);
  18.     void pop();
  19.     T get_top();
  20.     void show();
  21.     bool isFull() { return top >= MAX - 1; }
  22.     bool isEmpty() { return top < 0; }
  23. };
  24.  
  25. template <typename T>
  26. void myStack<T>::stack_initialize() {
  27.     top = -1;
  28. }
  29.  
  30. template <typename T>
  31. void myStack<T>::push(T X) {
  32.     if (this->isFull()) {
  33.         cout << "\nOverFlow Error : Your stack is full.\n";
  34.     }
  35.     else {
  36.         top++;
  37.         store[top] = X;
  38.         cout << "\nPush done..!" << endl;
  39.     }
  40. }
  41.  
  42. template <typename T>
  43. void myStack<T>::pop() {
  44.     if (this->isEmpty()) {
  45.         cout << "\nUnderFlow Error : Your stack is empty.\n";
  46.     }
  47.     else {
  48.         top--;
  49.         cout << "\nPop done..!" << endl;
  50.     }
  51. }
  52.  
  53. template <typename T>
  54. T myStack<T>::get_top() {
  55.     return store[top];
  56. }
  57.  
  58. template <typename T>
  59. void myStack<T>::show() {
  60.     for (int i = top; i >= 0; i--) {
  61.         cout << "|" << store[i] << "|" << endl;
  62.     }
  63. }
  64.  
  65. int main() {
  66.     myStack<string> s1; int A; bool flag = true; string x;
  67.     s1.stack_initialize();
  68.     while (flag) {
  69.         cout << "\nPush(1) / Pop(2) / Get top(3) / Show all(4) / End(5+)? : ";
  70.         cin >> A;
  71.         switch (A) {
  72.         case 1: cout << "\nPush what? : ";
  73.             cin >> x;
  74.             s1.push(x);
  75.             break;
  76.         case 2: s1.pop();
  77.             break;
  78.         case 3: cout << "\nTop is : " << s1.get_top() << endl;
  79.             break;
  80.         case 4: s1.show();
  81.             break;
  82.         default: cout << "\nEnd of the Stack Machine." << endl;
  83.             flag = false;
  84.         }
  85.     }
  86.     return 0;
  87. }
RAW Paste Data