Advertisement
DanikKUL

kekw

Nov 24th, 2022
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. //
  2. // Created by Dan Kulakovich on 25.10.22.
  3. //
  4.  
  5. #include <iostream>
  6. #include "ExceptionEmpty.h"
  7.  
  8. template <typename T> class Queue;
  9. template <typename T> std::ostream& operator << (std::ostream&, Queue<T>);
  10.  
  11. template <typename T>
  12. class Queue {
  13.  
  14.     struct Node {
  15.         Node *next;
  16.         T value;
  17.         explicit Node(T value = T(), Node *next = nullptr){
  18.             this -> value = value;
  19.             this -> next = next;
  20.         }
  21.     };
  22.     Node *node;
  23.     int size;
  24.  
  25. public:
  26.  
  27.     explicit Queue();
  28.     Queue(const Queue&);
  29.     ~Queue();
  30.  
  31.     void push(T value);
  32.     T pop();
  33.     T& front();
  34.     T& back();
  35.     int length();
  36.     bool isEmpty();
  37.  
  38.     friend std::ostream& operator << <> (std::ostream& out, Queue<T> queue) {
  39.         out << "[";
  40.         if (queue.size > 0) {
  41.             Node* tmp = queue.node -> next;
  42.             for (int i = 0; i < queue.length() - 1; i++) {
  43.                 out << tmp -> value << ", ";
  44.                 tmp = tmp -> next;
  45.             }
  46.             out << tmp -> value << "]";
  47.         } else {
  48.             out << "]";
  49.         }
  50.         return out;
  51.     }
  52. };
  53.  
  54. template <typename T>
  55. Queue<T>::Queue(){
  56.     node = new Node;
  57.     this -> size = 0;
  58. }
  59.  
  60. template <typename T>
  61. Queue<T>::Queue(const Queue& queue) {
  62.     this -> node = new Node(queue.node -> value);
  63.     Node *temp = queue.node, *head = this -> node;
  64.     for (int i = 0; i < queue.size; ++i) {
  65.         head -> next = new Node(temp -> value);
  66.         head = head -> next;
  67.         temp = temp -> next;
  68.     }
  69.     this -> size = queue.size;
  70. }
  71.  
  72. template <typename T>
  73. Queue<T>::~Queue() {
  74.     while (!isEmpty()) {
  75.         Node* temp = this -> node;
  76.         this -> node = this -> node -> next;
  77.         delete temp;
  78.         this -> size--;
  79.     }
  80. }
  81.  
  82. template <typename T>
  83. void Queue<T>::push(T value) {
  84.     if (this -> size == 0) {
  85.         this -> node = new Node(value);
  86.         this -> size++;
  87.         return;
  88.     }
  89.     Node* tmp = this -> node;
  90.     while (tmp -> next != nullptr) tmp = tmp -> next;
  91.     tmp -> next = new Node(value);
  92.     this -> size++;
  93. }
  94.  
  95. template <typename T>
  96. T Queue<T>::pop() {
  97.     try {
  98.         if (isEmpty())
  99.             throw ExceptionEmpty();
  100.         T ret = this->node->value;
  101.         this->node = this->node->next;
  102.         this->size--;
  103.         return ret;
  104.     }
  105.     catch (ExceptionEmpty& e) {
  106.         e.getError();
  107.     }
  108. }
  109.  
  110. template <typename T>
  111. T& Queue<T>::front() {
  112.     return node -> value;
  113. }
  114.  
  115. template <typename T>
  116. T& Queue<T>::back() {
  117.     Node* back = this -> node;
  118.     while (back -> next != nullptr) {
  119.         back = back -> next;
  120.     }
  121.     return back -> value;
  122. }
  123.  
  124. template <typename T>
  125. int Queue<T>::length() {
  126.     return this->size;
  127. }
  128.  
  129. template <typename T>
  130. bool Queue<T>::isEmpty() {
  131.     return size == 0;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement