Advertisement
Guest User

Untitled

a guest
May 30th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. template <typename T>
  2. struct Node
  3. {
  4.     T info;
  5.     Node *next, *prev;
  6. };
  7.  
  8. template<typename T>
  9. class Queue
  10. {
  11. private:
  12.     Node<T> *begin, *end;
  13.     int queue_depth;
  14.  
  15. public:
  16.     Queue() {
  17.         begin = end = NULL;
  18.         queue_depth = 0;
  19.     }
  20.  
  21.     ~Queue() {
  22.         delete[] begin;
  23.     }
  24.  
  25.     Queue(const Queue &q) {
  26.         Node<T>* cur = q.begin;
  27.         this->begin = this->end = new Node<T>;
  28.         this->begin->info = cur->info;
  29.         this->queue_depth = 1;
  30.         cur = cur->prev;
  31.         while (cur != NULL) {
  32.             this->push_back(cur->info);
  33.             cur = cur->prev;
  34.         }
  35.     }
  36.  
  37.     Queue& operator =(const Queue &right) {
  38.         if (this == &right)
  39.             return *this;
  40.         Node<T>* cur = right.begin;
  41.         this->end = this->begin;
  42.         this->begin->prev = NULL;
  43.         this->begin->info = cur->info;
  44.         cur = cur->prev;
  45.         while (cur != NULL) {
  46.             this->push_back(cur->info);
  47.             cur = cur->prev;
  48.         }
  49.         return *this;
  50.     }
  51.  
  52.     void push_front(T a) {
  53.         Node<T>* cur = new Node<T>;
  54.         if (!queue_depth) {
  55.             begin = end = cur;
  56.             cur->next = cur->prev = NULL;
  57.             cur->info = a;
  58.             queue_depth = 1;
  59.             return;
  60.         }
  61.         begin->next = cur;
  62.         cur->prev = begin;
  63.         begin = cur;
  64.         cur->next = NULL;
  65.         cur->info = a;
  66.         queue_depth++;
  67.     }
  68.  
  69.     void push_back(T a) {
  70.         Node<T>* cur = new Node<T>;
  71.         if (!queue_depth) {
  72.             begin = end = cur;
  73.             cur->next = cur->prev = NULL;
  74.             cur->info = a;
  75.             queue_depth = 1;
  76.             return;
  77.         }
  78.         end->prev = cur;
  79.         cur->next = end;
  80.         end = cur;
  81.         cur->prev = NULL;
  82.         cur->info = a;
  83.         queue_depth++;
  84.     }
  85.  
  86.     void print_begin() {
  87.         Node<T>* cur = begin;
  88.         while (cur != NULL) {
  89.             cout << cur->info << endl;
  90.             cur = cur->prev;
  91.         }
  92.     }
  93.  
  94.     void print_end() {
  95.         Node<T>* cur = end;
  96.         while (cur != NULL) {
  97.             cout << cur->info << endl;
  98.             cur = cur->next;
  99.         }
  100.     }
  101.  
  102.     void var5() {
  103.         Node<T>* cur;
  104.         while (begin->info % 5 == 0 && begin->info % 2 != 0) {
  105.             begin = begin->prev;
  106.             begin->next = NULL;
  107.         }
  108.         cur = begin;
  109.         while (cur->prev->prev != NULL) {
  110.             if (cur->prev->info % 5 == 0 && cur->prev->info % 2 != 0) {
  111.                 cur->prev = cur->prev->prev;
  112.                 cur->prev->next = cur;
  113.             }
  114.             else
  115.                 cur = cur->prev;
  116.         }
  117.         if (cur->prev->info % 5 == 0 && cur->prev->info % 2 != 0) {
  118.             end = end->next;
  119.             cur->prev = cur->prev->prev;
  120.         }
  121.     }
  122.  
  123.     void var11() {
  124.         Node<T>* cur = begin;
  125.         T sum = 0;
  126.         while (cur != NULL) {
  127.             sum += cur->info;
  128.             cur = cur->prev;
  129.         }
  130.         begin->info = T(sum / queue_depth);
  131.     }
  132. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement