fork download
  1. #include <iostream>
  2.  
  3. template<typename T>
  4. class DCLL
  5. {
  6. // internal representation of a Node
  7. struct Node
  8. {
  9. T m_value;
  10. Node* m_next;
  11. Node* m_prev;
  12.  
  13. inline Node(T value, Node* prev, Node* next)
  14. {
  15. m_value = value;
  16. m_prev = prev;
  17. m_next = next;
  18. if (m_prev)
  19. m_prev->m_next = this;
  20. if (m_next)
  21. m_next->m_prev = this;
  22. }
  23. };
  24.  
  25. Node* m_head;
  26. Node* m_tail;
  27.  
  28. public:
  29. DCLL() : m_head(nullptr), m_tail(nullptr) {}
  30.  
  31. void push_front(T val)
  32. {
  33. m_head = new Node(val, nullptr, m_head);
  34. if (!m_tail)
  35. m_tail = m_head;
  36. }
  37.  
  38. void push_back(T val)
  39. {
  40. m_tail = new Node(val, m_tail, nullptr);
  41. if (!m_head)
  42. m_head = m_tail;
  43. }
  44.  
  45. void walk() const {
  46. for (Node* node = m_head; node != nullptr; node = node->m_next) {
  47. std::cout << node->m_value << ' ';
  48. }
  49. std::cout << '\n';
  50. }
  51. };
  52.  
  53. int main()
  54. {
  55. DCLL<int> ints;
  56. ints.push_front(3);
  57. ints.push_front(2);
  58. ints.push_front(1);
  59. ints.push_back(4);
  60. ints.push_back(5);
  61. ints.push_back(6);
  62.  
  63. ints.walk();
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6