list.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef __LIST_H__
  2. #define __LIST_H__
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <memory>
  7. template<typename T>
  8. class list {
  9. public:
  10. struct list_node {
  11. T* _value;
  12. list_node* _prev, * _next;
  13. list_node() : _prev(nullptr), _next(nullptr) { _value = nullptr; }
  14. list_node(list_node* prev, list_node* next) : _prev(prev), _next(next) { _value = nullptr; }
  15. list_node(const T& value, list_node* prev = nullptr, list_node* next = nullptr) : _prev(prev), _next(next) { _value = new T(value); }
  16. T& operator*() { return *_value; }
  17. };
  18. class iterator {
  19. private:
  20. list_node* _ptr;
  21. // TBD 其它信息
  22. public:
  23. iterator(list_node* ptr);
  24. iterator& operator++();
  25. iterator& operator--();
  26. bool operator== (const iterator& it);
  27. bool operator!= (const iterator& it);
  28. T& operator*() { return **_ptr; }
  29. };
  30. private:
  31. list_node dummy;
  32. list_node* head = &dummy;
  33. list_node* tail = &dummy;
  34. public:
  35. list();
  36. public:
  37. iterator& operator[] (int index);
  38. list& operator+= (const T& value);
  39. public:
  40. iterator begin();
  41. iterator end();
  42. iterator rbegin();
  43. iterator rend();
  44. };
  45. #endif