Advertisement
Guest User

Untitled

a guest
Dec 27th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. template<typename T>
  2. class MyStack2 {
  3.     class Node {
  4.         friend class MyStack2;
  5.     public:
  6.         T data;
  7.         Node* next;
  8.     };
  9.     Node *head;
  10.     size_t topIndex;
  11. public:
  12.     MyStack2() { head = nullptr; topIndex = 0; };
  13.  
  14.     void Push2(T d) {
  15.         Node *pv = new Node;
  16.         pv->data = d;
  17.         pv->next = head;
  18.         head = pv;
  19.         topIndex++;
  20.     };
  21.  
  22.     T Pop2() {
  23.         topIndex--;
  24.         if (topIndex >= 0) {
  25.             T tmp = head->data;
  26.             Node* p_del = head;
  27.             head = head->next;
  28.             delete p_del;
  29.             return tmp;
  30.         }
  31.         else { throw "error"; }
  32.     };
  33.    
  34.     void MyStack2::Clear_MyStack2()
  35.     {
  36.         if (topIndex < 1) {
  37.             delete head;
  38.         }
  39.         else {
  40.             Node* pt1 = head;
  41.             Node* pt2 = head->next;
  42.  
  43.             while (pt2 != nullptr) {
  44.                 delete pt1;
  45.                 pt1 = pt2;
  46.                 pt2 = pt2->next;
  47.             }
  48.             topIndex = 0;
  49.         }
  50.        
  51.     };
  52.  
  53.  
  54.         MyStack2& operator=(const MyStack2& ref) {
  55.         if (this != &ref) {
  56.             //оптимизация
  57.             if (topIndex >= ref.topIndex) { //если размер текущего массива >= копируемого,
  58.  
  59.                 Node* pt1 = head;
  60.                 Node* pt2 = ref.head;
  61.  
  62.                 for (int i = 0; i < ref.topIndex; ++i) {//то перезаписываем элементы текущего элемента,
  63.                     *(pt1 + i) = *(pt2 + i);
  64.                 }
  65.  
  66.                 for (int i = ref.topIndex; i < topIndex; ++i) {//остальные удаляем
  67.                     delete (pt1 + i);
  68.                 }              
  69.             }
  70.             else {
  71.                 this->Clear_MyStack2();
  72.                 this->head = new T[ref.topIndex + 1]; // ??? '=' cannot convert from 'int *' to ...
  73.                 topIndex = ref.topIndex;
  74.                 Node* pt1 = head;
  75.                 Node* pt2 = ref.head;
  76.  
  77.                 for (int i = 0; i < topIndex; ++i) {
  78.                     *(pt1 + i) = *(pt2 + i);
  79.                 }
  80.             }
  81.         }
  82.         return *this;
  83.     };
  84.  
  85.     MyStack2& operator=(MyStack2&& ref) {
  86.         this->Clear_MyStack2();
  87.         if (ref.topIndex) {
  88.             head = ref.head;
  89.         }
  90.         return this;
  91.     };
  92.  
  93.     MyStack2(const MyStack2& ref) {
  94.         *this = ref;
  95.     };
  96.  
  97.     MyStack2(MyStack2&& ref) {
  98.         this->Clear_MyStack2();
  99.         if (ref.topIndex) {
  100.             head = ref.head;
  101.         }
  102.     };
  103. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement