Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // implements a doubly-linked list
  4. template <class T>
  5. class MyList
  6. {
  7. class Node
  8. {
  9. public:
  10. T *data;
  11. Node *prev;
  12. Node *next;
  13.  
  14. Node()
  15. : data(NULL)
  16. , prev(NULL)
  17. , next(NULL)
  18. {
  19. return;
  20. }
  21.  
  22. Node(T const& _data)
  23. : data(new T(_data))
  24. , prev(NULL)
  25. , next(NULL)
  26. {
  27. return;
  28. }
  29.  
  30. Node(T const& _data, Node *_prev, Node *_next)
  31. : data(new T(_data))
  32. , prev(_prev)
  33. , next(_next)
  34. {
  35. return;
  36. }
  37.  
  38. ~Node()
  39. {
  40. // memory leaks are bad, mmmk
  41. delete data;
  42. }
  43.  
  44. void insert_after(Node *node)
  45. {
  46. // hold onto next so we don't lose it
  47. Node *next_ref = next;
  48.  
  49. // next one after us is now the new node we're inserting
  50. next = node;
  51.  
  52. // new node's next becomes the old next
  53. next->next = next_ref;
  54.  
  55. // new node's prev becomes me
  56. next->prev = this;
  57. }
  58. };
  59.  
  60. // stores the size of the linked list
  61. size_t _size;
  62.  
  63. // head and tail pointers
  64. // simply point to next and prev nodes, respectively, do not store data
  65. Node *head;
  66. Node *tail;
  67.  
  68. public:
  69. MyList();
  70. ~MyList();
  71.  
  72. // push new data onto the front
  73. void push_front(T const& data);
  74.  
  75. // push new data onto the back
  76.  
  77. };
  78.  
  79. template<class T>
  80. MyList<T>::MyList()
  81. : _size(0)
  82. , head(new Node())
  83. , tail(new Node())
  84. {
  85. head->next = tail;
  86. tail->prev = head;
  87. return;
  88. }
  89.  
  90. template<class T>
  91. MyList<T>::~MyList()
  92. {
  93. delete head;
  94. delete tail;
  95. }
  96.  
  97. template<class T>
  98. MyList<T>::push_front(T const& data)
  99. {
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement