Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #ifndef STACK_H_INCLUDED
  2. #define STACK_H_INCLUDED
  3.  
  4. #include <cstdlib>
  5. #include <stdexcept>
  6. #include <iostream>
  7.  
  8. template <class T>
  9. class Stack;
  10.  
  11. template <class T>
  12. std::ostream& operator<<(std::ostream& os, const Stack<T> &s);
  13.  
  14. template <class T>
  15. class Stack {
  16. private:
  17.     T *data;
  18.     size_t sz;
  19.     size_t limit;
  20. public:
  21.  
  22.     Stack();
  23.     explicit Stack(size_t n);
  24.  
  25.     Stack(const Stack &other);
  26.     Stack& operator=(const Stack& other);
  27.  
  28.     Stack(Stack &&other);
  29.     Stack& operator=(Stack &&other);
  30.  
  31.     ~Stack();
  32.  
  33.     void Push(const T &val);
  34.     T& Pop();
  35.  
  36.     size_t size() {return sz;}
  37.  
  38.     friend std::ostream& operator<< <> (std::ostream& os, const Stack<T> &s);
  39.  
  40. };
  41.  
  42. template <class T>
  43. std::ostream& operator<<(std::ostream& os, const Stack<T> &s){
  44.     os << "size: " << s.sz << std::endl;
  45.     os << "limit: " << s.limit << std::endl;
  46.     for (size_t i = 0; i != s.sz; ++i)
  47.             os << s.data[i] << " ";
  48.     os << std::endl;
  49.  
  50.     return os;
  51. }
  52.  
  53. template <class T>
  54. Stack<T>::Stack() {
  55.     limit = 1;
  56.     data = new T[limit];
  57.     sz = 0;
  58. }
  59.  
  60. template <class T>
  61. Stack<T>::Stack(size_t n) {
  62.     limit = n;
  63.     data = new T[limit];
  64.     sz = n;
  65.  
  66.     for (size_t i = 0; i != sz; ++i)
  67.         data[i] = 0;
  68. }
  69.  
  70.  
  71. template <class T>
  72. Stack<T>::Stack(const Stack &other) {
  73.     limit = other.limit;
  74.     data = new T[limit];
  75.     sz = other.sz;
  76.     for (size_t i = 0; i != sz; ++i)
  77.         data[i] = other.data[i];
  78. }
  79.  
  80.  
  81. template <class T>
  82. Stack<T>& Stack<T>::operator=(const Stack &other) {
  83.     if (&other != this) {
  84.         delete[] data;
  85.  
  86.         limit = other.limit;
  87.         data = new T[limit];
  88.         sz = other.sz;
  89.         for (size_t i = 0; i != sz; ++i)
  90.             data[i] = other.data[i];
  91.     }
  92.  
  93.     return *this;
  94. }
  95.  
  96. template <class T>
  97. Stack<T>::Stack(Stack &&other) {
  98.     limit = other.limit;
  99.     data = other.data;
  100.     sz = other.sz;
  101.  
  102.     other.data = nullptr;
  103.     other.limit = other.sz = 0;
  104. }
  105.  
  106.  
  107. template <class T>
  108. Stack<T>& Stack<T>::operator=(Stack &&other) {
  109.     if (&other != this) {
  110.         delete[] data;
  111.  
  112.         limit = other.limit;
  113.         data = other.data;
  114.         sz = other.sz;
  115.  
  116.         other.data = nullptr;
  117.         other.limit = other.sz = 0;
  118.     }
  119.  
  120.     return *this;
  121. }
  122.  
  123. template <class T>
  124. Stack<T>::~Stack() {
  125.     delete[] data;
  126. }
  127.  
  128. template <class T>
  129. void Stack<T>::Push(const T &val) {
  130.     if (sz == limit || limit == 0) {
  131.         T *newData = new T[limit * 2];
  132.         limit *= 2;
  133.  
  134.         for (size_t i = 0; i != sz; ++i)
  135.             newData[i] = data[i];
  136.  
  137.         delete[] data;
  138.         data = newData;
  139.     }
  140.  
  141.     data[sz++] = val;
  142. }
  143.  
  144. template <class T>
  145. T& Stack<T>::Pop() {
  146.     if (sz == 0)
  147.         throw std::underflow_error("stack underflow");
  148.  
  149.     return data[--sz];
  150. }
  151.  
  152.  
  153.  
  154. #endif // STACK_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement