CircleList.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "List/List.h"
  2. #include <iostream>
  3. template <typename T>
  4. class CircleList: public List<T>
  5. {
  6. public:
  7. Node<T>* get_start() {
  8. return this->start;
  9. }
  10. CircleList() {}
  11. ~CircleList()
  12. {
  13. Node<T>* currentNode = this->start;
  14. for(int i = 0; i < this->lenght; i++)
  15. {
  16. currentNode = currentNode->next;
  17. delete currentNode->prev;
  18. }
  19. this->start = nullptr;
  20. this->end = nullptr;
  21. }
  22. void add (T value) {
  23. Node<T>* node = new Node<T>;
  24. node->value = value;
  25. if(this->start==nullptr) {
  26. this->start = node;
  27. this->end = node;
  28. return;
  29. }
  30. node->prev = this->end;
  31. node->next = this->start;
  32. this->end->next = node;
  33. this->start->prev = node;
  34. this->end = node;
  35. this->lenght++;
  36. }
  37. void add (T value, int index) {
  38. this->List<int>::add(value, index);
  39. }
  40. void pop(Node<T>* n) {
  41. if (n == this->start) this->start = this->start->next;
  42. this->List<int>::pop(n);
  43. }
  44. void pop() {
  45. this->end->prev->next = this->start;
  46. this->end = this->end->prev;
  47. this->lenght--;
  48. }
  49. void pop(int index) {
  50. this->List<int>::pop(index);
  51. }
  52. friend std::ostream &operator <<(std::ostream &os, CircleList &c)
  53. {
  54. os << "( ";
  55. Node<T>* currentNode = c.start;
  56. for(int i = 0; i < c.lenght; i++)
  57. {
  58. os << currentNode->value << " = ";
  59. currentNode = currentNode->next;
  60. }
  61. os << currentNode->value << "... )";
  62. return os;
  63. }
  64. };