CircleList.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. void add (T value) override{
  12. Node<T>* node = new Node<T>;
  13. node->value = value;
  14. if(this->start==nullptr) {
  15. this->start = node;
  16. this->end = node;
  17. return;
  18. }
  19. node->prev = this->end;
  20. node->next = this->start;
  21. this->end->next = node;
  22. this->start->prev = node;
  23. this->end = node;
  24. this->lenght++;
  25. }
  26. void add (T value, int index) override{
  27. static_cast<List<T>*>(this)->add(value, index);
  28. return;
  29. }
  30. void pop(Node<T>* n) {
  31. this->pop(n);
  32. }
  33. void pop() {
  34. this->end->prev->next = this->start;
  35. this->end = this->end->prev;
  36. this->lenght--;
  37. }
  38. void pop(int index) {
  39. this->pop(index);
  40. }
  41. friend std::ostream &operator <<(std::ostream &os, CircleList &c)
  42. {
  43. os << "( ";
  44. Node<T>* currentNode = c.start;
  45. for(int i = 0; i < c.lenght; i++)
  46. {
  47. os << currentNode->value << " = ";
  48. currentNode = currentNode->next;
  49. }
  50. os << currentNode->value << "... )";
  51. return os;
  52. }
  53. };