Advertisement
Guest User

Untitled

a guest
May 4th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 KB | None | 0 0
  1. #ifndef SLINKEDLIST_H
  2. #define SLINKEDLIST_H
  3. #include <iostream>
  4. #include "SNode.h"
  5. #include <stdexcept>
  6. #include "SLinkedIterator.h"
  7. #include "SLinkedIteratorConst.h"
  8.  
  9. template <typename Type>
  10. using iterator = SLinkedIterator<Type>;
  11.  
  12. template <typename Type>
  13. using const_iterator = SLinkedIteratorConst<Type>;
  14.  
  15. template <typename Type>
  16. using Node = SNode<Type>;
  17.  
  18.  
  19. template <typename Type>
  20. class SLinkedList {
  21.     private :
  22.         Node<Type> *head;
  23.         Node<Type> *tail;
  24.         size_t      count;
  25.     public  :
  26.         SLinkedList();
  27.         SLinkedList(const SLinkedList& list);
  28.         SLinkedList(SLinkedList&& list);
  29.         ~SLinkedList();
  30.  
  31.         SLinkedList& operator=(const SLinkedList& list);
  32.         SLinkedList& operator=(SLinkedList&& list);
  33.  
  34.         void push_back(const Type& value);
  35.         void push_back(Type&& value);
  36.  
  37.         void push_front(const Type& value);
  38.         void push_front(Type&& value);
  39.  
  40.         void pop_back();
  41.         void pop_front();
  42.  
  43.         Type& front();
  44.         const Type& front() const;
  45.  
  46.         Type& back();
  47.         const Type& back() const;
  48.  
  49.         size_t size()  const;
  50.         bool   empty() const;
  51.         void   print() const;
  52.         void   clear();
  53.  
  54.         iterator<Type> begin();
  55.         iterator<Type> end();
  56.  
  57.         const_iterator<Type> begin() const;
  58.         const_iterator<Type> end() const;
  59.  
  60.         iterator<Type> insert(iterator<Type> position, const Type& value);
  61.         iterator<Type> insert(iterator<Type> position, Type&& value);
  62. };
  63.  
  64. template<typename Type>
  65. iterator<Type> SLinkedList<Type>::begin() { return iterator<Type>(head);}
  66.  
  67. template<typename Type>
  68. iterator<Type> SLinkedList<Type>::end() { return iterator<Type>(tail->getNext());}
  69.  
  70. template<typename Type>
  71. const_iterator<Type> SLinkedList<Type>::begin() const { return const_iterator<Type>(head);}
  72.  
  73. template<typename Type>
  74. const_iterator<Type> SLinkedList<Type>::end() const { return const_iterator<Type>(tail->getNext());}
  75.  
  76. template<typename Type>
  77. SLinkedList<Type>::SLinkedList() : head(nullptr), tail(nullptr), count(0) {}
  78.  
  79. template<typename Type>
  80. iterator<Type> SLinkedList<Type>::insert(iterator<Type> position, const Type& value) {
  81.     Node<Type> *newNode = new Node<Type>(value,position.current->getNext());
  82.     Node<Type> *temp    = head;
  83.     while(temp != position.current) {
  84.         temp = temp->getNext();
  85.     }
  86.  
  87.     temp->setNext(newNode);
  88. }
  89.  
  90. template<typename Type>
  91. iterator<Type> SLinkedList<Type>::insert(iterator<Type> position, Type&& value) {
  92.     Node<Type> *newNode = new Node<Type>(value,position.current->getNext());
  93.     Node<Type> *temp    = head;
  94.     while(temp != position.current) {
  95.         temp = temp->getNext();
  96.     }
  97.  
  98.     temp->setNext(newNode);
  99. }
  100.  
  101. template<typename Type>
  102. SLinkedList<Type>::SLinkedList(const SLinkedList& list ) {
  103.     count = list.count;
  104.     head  = new Node<Type>(list.head->getInfo(), nullptr);
  105.     tail  = new Node<Type>
  106.  
  107.     Node<Type> *temp = list;
  108.     while(temp) {
  109.         push_back()
  110.     }
  111. }
  112.  
  113. template<typename Type>
  114. SLinkedList<Type>::SLinkedList(SLinkedList&& list) {
  115.  
  116. }
  117.  
  118. template<typename Type>
  119. SLinkedList<Type>::~SLinkedList(){
  120.     clear();
  121. }
  122.  
  123. template<typename Type>
  124. SLinkedList<Type>& SLinkedList<Type>::operator=(const SLinkedList& list) {
  125.  
  126.     return *this;
  127. }
  128.  
  129. template<typename Type>
  130. SLinkedList<Type>& SLinkedList<Type>::operator=(SLinkedList&& list) {
  131.  
  132.     return *this;
  133. }
  134.  
  135. template<typename Type>
  136. void SLinkedList<Type>::push_back(const Type& value) {
  137.     Node<Type> *newNode = new Node<Type>(value, nullptr);
  138.     if(!head) {
  139.         head = newNode;
  140.         tail = newNode;
  141.     }
  142.     else {
  143.         Node<Type> *temp = head;
  144.         while(temp->getNext()) {
  145.             temp = temp->getNext();
  146.         }
  147.  
  148.         temp->setNext(newNode);
  149.         tail = newNode;
  150.     }
  151.     ++count;
  152. }
  153.  
  154. template<typename Type>
  155. void SLinkedList<Type>::push_back(Type&& value) {
  156.     Node<Type> *newNode = new Node<Type>(value, nullptr);
  157.     if(!head) {
  158.         head = newNode;
  159.         tail = newNode;
  160.     }
  161.     else {
  162.         Node<Type> *temp = head;
  163.         while(temp->getNext()) {
  164.             temp = temp->getNext();
  165.         }
  166.  
  167.         temp->setNext(newNode);
  168.         tail = newNode;
  169.     }
  170.     ++count;
  171. }
  172.  
  173. template<typename Type>
  174. void SLinkedList<Type>::push_front(const Type& value) {
  175.     Node<Type> *newNode = new Node<Type>(value, nullptr);
  176.     if(!head) {
  177.         head = newNode;
  178.         tail = newNode;
  179.     }
  180.     else{
  181.         newNode->setNext(head);
  182.         head = newNode;
  183.     }
  184.     ++count;
  185. }
  186.  
  187. template<typename Type>
  188. void SLinkedList<Type>::push_front(Type&& value) {
  189.     Node<Type> *newNode = new Node<Type>(value, nullptr);
  190.     if(!head) {
  191.         head = newNode;
  192.         tail = newNode;
  193.     }
  194.     else{
  195.         newNode->setNext(head);
  196.         head = newNode;
  197.     }
  198.     ++count;
  199. }
  200.  
  201. template<typename Type>
  202. void SLinkedList<Type>::pop_back() {
  203.     Node<Type> *newNode = head;
  204.     while(newNode->getNext()->getNext())
  205.         newNode = newNode->getNext();
  206.     tail = newNode;
  207.     tail->setNext(nullptr);
  208.     newNode = newNode->getNext();
  209.     delete newNode;
  210.  
  211.     --count;
  212. }
  213.  
  214. template<typename Type>
  215. void SLinkedList<Type>::pop_front() {
  216.     Node<Type> *newNode = head;
  217.     head = head->getNext();
  218.     delete newNode;
  219. }
  220.  
  221. template<typename Type>
  222. Type& SLinkedList<Type>::front() {
  223.     return head->getInfo();
  224. }
  225.  
  226. template<typename Type>
  227. const Type& SLinkedList<Type>::front() const {
  228.     return head->getInfo();
  229. }
  230.  
  231. template<typename Type>
  232. Type& SLinkedList<Type>::back() {
  233.     return tail->getInfo();
  234. }
  235.  
  236. template<typename Type>
  237. const Type& SLinkedList<Type>::back() const {
  238.     return tail->getInfo();
  239. }
  240.  
  241. template<typename Type>
  242. size_t SLinkedList<Type>::size() const {
  243.     return count;
  244. }
  245.  
  246. template<typename Type>
  247. bool SLinkedList<Type>::empty() const {
  248.     return (head == nullptr);
  249. }
  250.  
  251. template<typename Type>
  252. void SLinkedList<Type>::print() const {
  253.     Node<Type> *temp = head;
  254.     while(temp) {
  255.         std::cout << temp->getInfo() << std::endl;
  256.         temp = temp->getNext();
  257.     }
  258. }
  259.  
  260. template<typename Type>
  261. void SLinkedList<Type>::clear() {
  262.     Node<Type> *temp = head;
  263.     while(head->getNext()) {
  264.         pop_front();
  265.     }
  266.     count = 0;
  267. }
  268.  
  269.  
  270.  
  271. #endif // SLINKEDLIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement