Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. template<typename T>
  2. class Stack{
  3. int n;
  4. T* arr;
  5. int m;
  6. public:
  7. Stack(int n = 16){
  8. this->n = n;
  9. arr = new T[n];
  10. m = 0;
  11. }
  12. void push(T val){
  13. if(m == n - 1){
  14. cout << "Stack overflow" << endl;
  15. exit(1);
  16. }
  17. arr[m] = val;
  18. m++;
  19. }
  20. void pop(){
  21. if(!isEmpty()){
  22. m--;
  23. }else{
  24. exit(1);
  25. }
  26.  
  27. }
  28. void top(){
  29. return arr[m];
  30. }
  31. bool isEmpty(){
  32. return m == 0 ? true : false;
  33. }
  34. int size(){
  35. return m;
  36. }
  37. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement