Advertisement
pVinc

stos.cpp

Feb 26th, 2020
151
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. template <class T> class Stos {
  7.     T *stos;
  8.     unsigned int pos;
  9.     unsigned int stack_size;
  10. public:
  11.     Stos(unsigned int ssiz){
  12.         stos = (T*)calloc(ssiz, sizeof(T));
  13.         pos = 0;
  14.         stack_size = ssiz;
  15.     };
  16.  
  17.     void push(T a){
  18.         if(pos+1 > stack_size){ cout << "stack size exceeded" << endl; return; }
  19.         stos[pos] = a;
  20.         pos++;
  21.     }
  22.  
  23.     T pop(){
  24.         if(pos-1 < 0){ cout << "hit stack bottom" << endl; return NULL; }
  25.         return stos[--pos];
  26.     }
  27. };
  28.  
  29. int main(){
  30.     Stos<string> a(2);
  31.     a.push("Lorem");
  32.     a.push("ipsum");
  33.     cout << a.pop() << endl << a.pop() << endl;
  34.     return 0;
  35. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement