Advertisement
Skydive

LinkedList.cpp

Jul 4th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include "LinkedList.h"
  2.  
  3. template<class T>
  4. LinkedList<T>::LinkedList()
  5. {
  6.     h_node = nullptr;
  7.     length=0;
  8. }
  9.  
  10. template<class T>
  11. LinkedList<T>::~LinkedList()
  12. {
  13.     LinkedNode<T>* conductor = h_node;
  14.     while(conductor->next != h_node)
  15.     {
  16.         conductor = conductor->next;
  17.         delete conductor;
  18.     }
  19.     delete h_node;
  20. }
  21.  
  22. template<class T>
  23. LinkedNode<T>* LinkedList<T>::push_back(T value)
  24. {
  25.     LinkedNode<T>* conductor = h_node;
  26.  
  27.  
  28.     if(h_node == nullptr)
  29.     {
  30.         h_node = new LinkedNode<T>;
  31.         h_node->x = value;
  32.         h_node->next = h_node;
  33.         length++;
  34.         return h_node;
  35.     }
  36.  
  37.     while(conductor->next != h_node)
  38.     {
  39.         conductor = conductor->next;
  40.     }
  41.  
  42.     conductor->next = new LinkedNode<T>;
  43.     conductor->next->x = value;
  44.     conductor->next->next = h_node;
  45.     length++;
  46.     return conductor->next;
  47. }
  48.  
  49. template<class T>
  50. LinkedNode<T>* LinkedList<T>::push_front(T value)
  51. {
  52.     if(h_node == nullptr)
  53.     {
  54.         h_node = new LinkedNode<T>;
  55.         h_node->x = value;
  56.         h_node->next = h_node;
  57.         length++;
  58.         return h_node;
  59.     }
  60.  
  61.     LinkedNode<T>* old_h = h_node;
  62.     h_node = new LinkedNode<T>;
  63.     h_node->x = value;
  64.     h_node->next = old_h;
  65.     length++;
  66.  
  67.     LinkedNode<T>* conductor = old_h;
  68.     while(conductor->next != old_h)
  69.     {
  70.         conductor = conductor->next;
  71.     }
  72.     conductor->next = h_node;
  73.     return h_node;
  74. }
  75.  
  76. template<class T>
  77. void LinkedList<T>::set_head(LinkedNode<T>* node)
  78. {
  79.     LinkedNode<T>* conductor = h_node;
  80.     int i=0;
  81.     while ( conductor->next != node )
  82.     {
  83.         conductor = conductor->next;
  84.  
  85.         if(i > length)
  86.             return;
  87.         i++;
  88.     }
  89.     h_node = node;
  90. }
  91.  
  92. template<class T>
  93. LinkedNode<T>* LinkedList<T>::get_head()
  94. {
  95.     return h_node;
  96. }
  97.  
  98. template<class T>
  99. void LinkedList<T>::remove(LinkedNode<T>* node)
  100. {
  101.     LinkedNode<T>* conductor = h_node;
  102.     LinkedNode<T>* prev_node;
  103.     int i=0;
  104.     while ( conductor->next != node )
  105.     {
  106.         prev_node = conductor;
  107.         conductor = conductor->next;
  108.  
  109.         if(i > length)
  110.             return;
  111.         i++;
  112.     }
  113.     // TODO: Why do an extra next. Why on earth does this work!?
  114.     prev_node->next->next = conductor->next->next;
  115.     if(node == h_node)
  116.     {
  117.         h_node = conductor->next->next;
  118.     }
  119.     delete node;
  120.     length--;
  121. }
  122.  
  123. template<class T>
  124. template<typename IterF>
  125. void LinkedList<T>::iterate(IterF iterfunction)
  126. {
  127.     LinkedNode<T>* conductor = h_node;
  128.  
  129.     iterfunction(h_node->x);
  130.     while ( conductor->next != h_node )
  131.     {
  132.         iterfunction(conductor->next->x);
  133.         conductor = conductor->next;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement