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. std::cout << currentNode->value << std::endl;
  17. currentNode = currentNode->next;
  18. delete currentNode->prev;
  19. }
  20. this->start = nullptr;
  21. this->end = nullptr;
  22. }
  23. void add (T value) {
  24. Node<T>* node = new Node<T>;
  25. node->value = value;
  26. if(this->start==nullptr) {
  27. this->start = node;
  28. this->end = node;
  29. return;
  30. }
  31. node->prev = this->end;
  32. node->next = this->start;
  33. this->end->next = node;
  34. this->start->prev = node;
  35. this->end = node;
  36. this->lenght++;
  37. }
  38. void add (T value, int index) {
  39. this->List<int>::add(value, index);
  40. }
  41. void pop(Node<T>* n) {
  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. };