Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template<typename T>
  4. class KDeque;
  5.  
  6. template<typename T>
  7. class IsUniqueVisitor
  8. {
  9. public:
  10.     void visit(KDeque<T>& deq)
  11.     {
  12.         std::set<T> st;
  13.         for (auto it = deq.begin(); it != deq.end(); ++it) {
  14.             st.insert(*it);
  15.         }
  16.         result = st.size() == deq.size();
  17.     }
  18.  
  19.     void getResult()
  20.     {
  21.         if (result)
  22.             std::cout << "Your deque has unique elements\n";
  23.         else
  24.             std::cout << "Your deque doesn't have unique elements\n";
  25.         std::cout << std::endl;
  26.     }
  27. private:
  28.     bool result;
  29. };
  30.  
  31. template<typename T>
  32. class KDeque
  33. {
  34. private:
  35.     class Node
  36.     {
  37.     public:
  38.  
  39.         T value;
  40.         Node *prev, *next;
  41.         Node() : value(0), prev(NULL), next(NULL) {}
  42.         Node(T& value) : value(value), prev(NULL), next(NULL) {}
  43.     };
  44.     Node *head, *tail;
  45.     int _size;
  46.  
  47. public:
  48.     class KIterator
  49.     {
  50.     public:
  51.         KIterator(Node* other);
  52.         KIterator operator++();
  53.         T& operator*() const;
  54.         bool operator!=(const KIterator& other);
  55.         Node* node;
  56.     };
  57.     friend class KIterator;
  58.     //typedef KIterator<T> iterator;
  59.     KDeque();
  60.     KDeque(std::initializer_list<T> args);
  61.     KDeque(KDeque<T>&& other);
  62.     KDeque(const KDeque<T>& other);
  63.     ~KDeque();
  64.  
  65.     bool isEmpty() const;
  66.     int size() const;
  67.     void clear();
  68.     T& front() const;
  69.     T& back() const;
  70.     void pushFront(T& element);
  71.     void pushBack(T element);
  72.     void popBack();
  73.     void popFront();
  74.     void swap(KDeque<T>& other);
  75.  
  76.     template<typename TT>
  77.     friend std::ostream& operator<<(std::ostream& out, KDeque<TT>& other);
  78.     bool operator==(const KDeque<T>& other) const;
  79.     bool operator!=(const KDeque<T>& other) const;
  80.     KDeque<T>& operator=(const KDeque<T>& other);
  81.     KDeque<T>& operator=(KDeque<T>&& other);
  82.     KDeque<T>& operator+=(const KDeque<T>& other);
  83.     KDeque<T> operator+(const KDeque<T>& other);
  84.  
  85.     KIterator begin();
  86.     KIterator end();
  87.  
  88.     void accept(IsUniqueVisitor<T>& visitor);
  89. };
  90.  
  91.  
  92. template<typename T>
  93. void KDeque<T>::accept(IsUniqueVisitor<T>& visitor)
  94. {
  95.     visitor.visit(*this);
  96. }
  97.  
  98. template<typename T>
  99. KDeque<T>::KDeque()
  100. {
  101.     this->_size = 0;
  102.     head = tail = NULL;
  103. }
  104.  
  105. template<typename T>
  106. KDeque<T>::KDeque(std::initializer_list<T> args) : KDeque()
  107. {
  108.     for (auto x : args)
  109.         pushBack(x);
  110. }
  111.  
  112. template<typename T>
  113. KDeque<T>::KDeque(KDeque<T>&& other) : _size(other._size), head(other.head), tail(other.tail)
  114. {
  115.     other.head = other.tail = nullptr;
  116.     other._size = 0;
  117. }
  118.  
  119. template<typename T>
  120. KDeque<T>::KDeque(const KDeque<T>& other) : KDeque()
  121. {
  122.     auto *cur = other.head;
  123.     while (cur != NULL) {
  124.         pushBack(cur->value);
  125.         cur = cur->next;
  126.     }
  127. }
  128.  
  129. template<typename T>
  130. KDeque<T>::~KDeque()
  131. {
  132.     clear();
  133. }
  134.  
  135. template<typename T>
  136. bool KDeque<T>::isEmpty() const
  137. {
  138.     return this->_size == 0;
  139. }
  140.  
  141. template<typename T>
  142. int KDeque<T>::size() const
  143. {
  144.     return this->_size;
  145. }
  146.  
  147. template<typename T>
  148. void KDeque<T>::clear()
  149. {
  150.     KDeque<T>::Node *nxt;
  151.     for (int i = 0; i < this->_size; i++) {
  152.         nxt = head->next;
  153.         delete head;
  154.         head = nxt;
  155.     }
  156.     head = tail = NULL;
  157.     this->_size = 0;
  158. }
  159.  
  160. template<typename T>
  161. T& KDeque<T>::front() const
  162. {
  163.     std::cout << "Your deque is empty!\n";
  164.     return head->value;
  165. }
  166.  
  167. template<typename T>
  168. T& KDeque<T>::back() const
  169. {
  170.     std::cout << "Your deque is empty!\n";
  171.     return tail->value;
  172. }
  173.  
  174. template<typename T>
  175. void KDeque<T>::pushFront(T& element)
  176. {
  177.     KDeque<T>::Node* newHead = new KDeque<T>::Node(element);
  178.     if (this->_size) {
  179.         head->prev = newHead;
  180.     }
  181.     else {
  182.         tail = newHead;
  183.     }
  184.     newHead->next = head;
  185.     head = newHead;
  186.     ++this->_size;
  187. }
  188.  
  189. template<typename T>
  190. void KDeque<T>::pushBack(T element)
  191. {
  192.     KDeque<T>::Node* newTail = new KDeque<T>::Node(element);
  193.     if (this->_size)
  194.         tail->next = newTail;
  195.     else
  196.         head = newTail;
  197.     newTail->prev = tail;
  198.     tail = newTail;
  199.     ++this->_size;
  200. }
  201.  
  202. template<typename T>
  203. void KDeque<T>::popBack()
  204. {
  205.     if (this->_size > 1) {
  206.         KDeque<T>::Node* newTail = tail->prev;
  207.         delete tail;
  208.         tail = newTail;
  209.         tail->next = NULL;
  210.         _size--;
  211.     }
  212.     else if (this->_size == 1) {
  213.         delete tail;
  214.         head = tail = NULL;
  215.         _size--;
  216.     }
  217.     else {
  218.         std::cout << "Nothing to pop. KDeque<T> is empty\n";
  219.     }
  220. }
  221.  
  222. template<typename T>
  223. void KDeque<T>::popFront()
  224. {
  225.     if (this->_size > 1) {
  226.         KDeque<T>::Node* newHead = head->next;
  227.         delete head;
  228.         head = newHead;
  229.         head->prev = NULL;
  230.         _size--;
  231.     }
  232.     else if (this->_size == 1) {
  233.         delete head;
  234.         head = tail = NULL;
  235.         _size--;
  236.     }
  237.     else {
  238.         std::cout << "Nothing to pop. KDeque<T> is empty\n";
  239.     }
  240. }
  241.  
  242. template<typename T>
  243. void KDeque<T>::swap(KDeque<T>& other)
  244. {
  245.     std::swap(head, other.head);
  246.     std::swap(tail, other.tail);
  247.     std::swap(_size, other._size);
  248. }
  249.  
  250. template<typename T>
  251. std::ostream& operator<<(std::ostream& out, KDeque<T>& other)
  252. {
  253.     auto cur = other.head;
  254.     while (cur != NULL) {
  255.         out << cur->value << " ";
  256.         cur = cur->next;
  257.     }
  258.     out << std::endl;
  259.     return out;
  260. }
  261.  
  262. template<typename T>
  263. bool KDeque<T>::operator==(const KDeque<T>& other) const {
  264.     KDeque<T>::Node *f = this->head,
  265.         *s = other.head;
  266.     if (this->_size != other._size)
  267.         return false;
  268.  
  269.     while (f != NULL && s != NULL) {
  270.         if (f->value != s->value)
  271.             return false;
  272.         f = f->next;
  273.         s = s->next;
  274.     }
  275.     return true;
  276. }
  277.  
  278. template<typename T>
  279. bool KDeque<T>::operator!=(const KDeque<T>& other) const {
  280.     return !((*this) == other);
  281. }
  282.  
  283. template<typename T>
  284. KDeque<T>& KDeque<T>::operator=(const KDeque<T>& other)
  285. {
  286.     if (this != &other) {
  287.         clear();
  288.         KDeque<T>::Node *cur = other.head;
  289.         while (cur != NULL) {
  290.             pushBack(cur->value);
  291.             cur = cur->next;
  292.         }
  293.     }
  294.     return (*this);
  295. }
  296.  
  297. template<typename T>
  298. KDeque<T>& KDeque<T>::operator=(KDeque<T>&& other)
  299. {
  300.     if (this != &other) {
  301.         clear();
  302.         _size = other._size;
  303.         head = other.head;
  304.         tail = other.tail;
  305.         other.head = other.tail = nullptr;
  306.         other._size = 0;
  307.     }
  308.     return *this;
  309. }
  310.  
  311. template<typename T>
  312. KDeque<T>& KDeque<T>::operator+=(const KDeque<T>& other)
  313. {
  314.     KDeque<T>::Node *cur = other.head;
  315.     while (cur != NULL) {
  316.         pushBack(cur->value);
  317.         cur = cur->next;
  318.     }
  319.     return (*this);
  320. }
  321.  
  322. template<typename T>
  323. KDeque<T> KDeque<T>::operator+(const KDeque<T>& other)
  324. {
  325.     KDeque *result = new KDeque;
  326.     (*result) = (*this);
  327.     (*result) += other;
  328.     return (*result);
  329. }
  330.  
  331.  
  332.  
  333. template<typename T>
  334. KDeque<T>::KIterator::KIterator(KDeque<T>::Node* other)
  335. {
  336.     node = other;
  337. }
  338.  
  339. template<typename T>
  340. typename KDeque<T>::KIterator KDeque<T>::KIterator::operator++()
  341. {
  342.     node = node->next;
  343.     return *this;
  344. }
  345.  
  346. template<typename T>
  347. T& KDeque<T>::KIterator::operator*() const
  348. {
  349.     return node->value;
  350. }
  351.  
  352. template<typename T>
  353. typename KDeque<T>::KIterator KDeque<T>::begin()
  354. {
  355.     return typename KDeque<T>::KIterator::KIterator(head);
  356. }
  357.  
  358. template<typename T>
  359. typename KDeque<T>::KIterator KDeque<T>::end()
  360. {
  361.     return typename KDeque<T>::KIterator::KIterator(tail->next);
  362. }
  363.  
  364. template<typename T>
  365. bool KDeque<T>::KIterator::operator!=(const KDeque<T>::KIterator& other)
  366. {
  367.     return this->node != other.node;
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement