Advertisement
jeanleopoldo

CircularList

Apr 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. //  Copyright [2017] <Jean Leopoldo>
  2. #ifndef STRUCTURES_CIRCULAR_LIST_H
  3. #define STRUCTURES_CIRCULAR_LIST_H
  4.  
  5. #include <cstdint>
  6. #include <stdexcept>
  7.  
  8. namespace structures {
  9.  
  10. template<typename T>
  11. //! class CircularList
  12. /*
  13. *  class CircularList
  14. */
  15. class CircularList {
  16.  public:
  17. //! constructor CircularList
  18. /*
  19. *  constructor CircularList
  20. */
  21.     CircularList() {
  22.         head = nullptr;
  23.         tail = nullptr;
  24.         size_ = 0;
  25.     }
  26. //! destructor CircularList
  27. /*
  28. *  destructor CircularList
  29. */
  30.     ~CircularList() {
  31.         clear();
  32.     }
  33. //! clear CircularList
  34. /*
  35. *  clear CircularList
  36. */
  37.     void clear() {
  38.         while (!empty()) {
  39.             pop_front();
  40.         }
  41.         head = nullptr;
  42.         tail = nullptr;
  43.         size_ = 0;
  44.     }
  45. //! push_back
  46. /*
  47. *  push_back
  48. */
  49.     void push_back(const T& data) {
  50.         Node *node = new Node(data);
  51.         if (node == nullptr) {
  52.             throw std::out_of_range("full");
  53.         } else if (empty()) {
  54.             push_front(data);
  55.         } else {
  56.             Node *aux = head;
  57.             for (int i = 0; i < size_-1; i++) {
  58.                 aux = aux->next();
  59.             }
  60.             size_++;
  61.             aux->next(node);
  62.             node->next(head);
  63.         }
  64.     }
  65. //! push_front
  66. /*
  67. *  push_front
  68. */
  69.     void push_front(const T& data) {
  70.         Node *node = new Node(data, head);
  71.         if (node == nullptr) {
  72.             throw std::out_of_range("full");
  73.         } else if (empty()) {
  74.             size_++;
  75.             head = node;
  76.             tail = node;
  77.             node->next(node);
  78.         } else {
  79.             size_++;
  80.             head = node;
  81.             tail->next(node);
  82.         }
  83.     }
  84. //! insert
  85. /*
  86. *  insert
  87. */
  88.     void insert(const T& data, std::size_t index) {
  89.         if (empty() || index == 1) {
  90.             push_front(data);
  91.         } else if (index == size_) {
  92.             push_back(data);
  93.         } else {
  94.             Node *node = new Node(data);
  95.             if (node == nullptr) {
  96.                 throw std::out_of_range("full");
  97.             } else {
  98.                 Node *aux = head;
  99.                 for (int i = 0; i < index; i++) {
  100.                     aux = aux->next();
  101.                 }
  102.                 node->next(aux->next());
  103.                 aux->next(node);
  104.                 delete aux;
  105.             }
  106.         }
  107.     }
  108. //! insert
  109. /*
  110. *  insert
  111. */
  112.     void insert_sorted(const T& data) {
  113.             sort();
  114.             Node *aux = head;
  115.             int i = 0;
  116.             for (; aux->data() < data; i++) {
  117.                 aux = aux->next();
  118.             }
  119.             insert(data, i);
  120.     }
  121. //! insert
  122. /*
  123. *  insert
  124. */
  125.     T& at(std::size_t index) {
  126.         if (empty()) {
  127.             throw std::out_of_range("empty");
  128.         } else {
  129.             Node *aux = head;
  130.             for (int i = 0; i < index; i++) {
  131.                 aux = aux->next();
  132.             }
  133.             T& data = aux->data();
  134.             delete aux;
  135.             return data;
  136.         }
  137.     }
  138. //! insert
  139. /*
  140. *  insert
  141. */
  142.     const T& at(std::size_t index) const {
  143.         if (empty()) {
  144.             throw std::out_of_range("empty");
  145.         } else {
  146.             Node *aux = head;
  147.             T& data;
  148.             for (int i = 0; i < index; i++) {
  149.                 aux = aux->next();
  150.             }
  151.             data = aux->data;
  152.             delete aux;
  153.             return data;
  154.         }
  155.     }
  156. //! insert
  157. /*
  158. *  insert
  159. */
  160.     T pop(std::size_t index) {
  161.         if (empty()) {
  162.             throw std::out_of_range("empty");
  163.         } else if (index == 1) {
  164.             return pop_front();
  165.         } else {
  166.             Node *aux = head;
  167.             Node *del;
  168.             T data;
  169.             for (int i = 0; i < index-1; i++) {
  170.                 aux = aux->next();
  171.             }
  172.             del = aux->next();
  173.             data = del->data();
  174.             aux->next(del->next());
  175.             delete aux;
  176.             delete del;
  177.             return data;
  178.         }
  179.     }
  180. //! insert
  181. /*
  182. *  insert
  183. */
  184.     T pop_back() {
  185.         return pop(size_);
  186.     }
  187. //! insert
  188. /*
  189. *  insert
  190. */
  191.     T pop_front() {
  192.         if (empty())
  193.             throw std::out_of_range("empty");
  194.         Node *aux = head;
  195.         head = aux->next();
  196.         T data = aux->data();
  197.         delete aux;
  198.         return data;
  199.     }
  200. //! insert
  201. /*
  202. *  insert
  203. */
  204.     void remove(const T& data) {
  205.         Node *aux = head;
  206.         for (int i = 0; i < size_-1; i++) {
  207.             if (aux->data() == data) {
  208.                 pop(i);
  209.             }
  210.         }
  211.         throw std::out_of_range("data has not been found");
  212.     }
  213. //! insert
  214. /*
  215. *  insert
  216. */
  217.     bool empty() const {
  218.         if (size_ == 0)
  219.             return true;
  220.         return false;
  221.     }
  222. //! insert
  223. /*
  224. *  insert
  225. */
  226.     bool contains(const T& data) const {
  227.         if (empty())
  228.             throw std::out_of_range("empty");
  229.         Node *aux = head;
  230.         for (int i = 0; i < size_-1; i++) {
  231.             if (aux->data() == data)
  232.                 return true;
  233.             aux = aux->next();
  234.         }
  235.         throw std::out_of_range("data has not been found");
  236.     }
  237. //! insert
  238. /*
  239. *  insert
  240. */
  241.     std::size_t find(const T& data) const {
  242.         if (empty())
  243.             throw std::out_of_range("empty");
  244.         Node *aux = head;
  245.         for (int i = 0; i < size_-1; i++) {
  246.             if (aux->data() == data)
  247.                 return i;
  248.             aux = aux->next();
  249.         }
  250.         throw std::out_of_range("data has not been found");
  251.     }
  252. //! insert
  253. /*
  254. *  insert
  255. */
  256.     std::size_t size() const {
  257.         return size_;
  258.     }
  259.  
  260.  private:
  261.     class Node {
  262.      public:
  263.         explicit Node(const T& data) {
  264.             data_ = data;
  265.             next_ = nullptr;
  266.         }
  267.  
  268.         Node(const T& data, Node *next) {
  269.             data_ = data;
  270.             next_ = next;
  271.         }
  272.  
  273.         Node *next() {
  274.             return next_;
  275.         }
  276.  
  277.         void next(Node *next) {
  278.             next_ = next;
  279.         }
  280.  
  281.         T& data() {
  282.             return data_;
  283.         }
  284.  
  285.      private:
  286.         Node *next_;
  287.         T data_;
  288.     };
  289.  
  290.     std::size_t size_;
  291.     Node *head;
  292.     Node *tail;
  293.  
  294.     void sort() {
  295.         Node *aux = head;
  296.         for (int i = 0; i < size_-1; i++) {
  297.             T data = aux->data();
  298.             for (int j = 0; j < size_ -1; j++) {
  299.                 aux = aux->next();
  300.                 if (aux->data() > data) {
  301.                     data = aux->data();
  302.                 }
  303.             }
  304.         }
  305.     }
  306. };
  307.  
  308. }  //  namespace structures
  309.  
  310. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement