Advertisement
Guest User

Together.cpp

a guest
Jul 4th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <stdlib.h>
  4.  
  5. template<class T>
  6. struct LinkedNode
  7. {
  8.     T x;
  9.     LinkedNode *next;
  10. };
  11.  
  12. template<class T>
  13. class LinkedList
  14. {
  15. private:
  16.     LinkedNode<T>* h_node;
  17.  
  18. public:
  19.     LinkedList();
  20.     ~LinkedList();
  21.     LinkedNode<T>* push_front(T value);
  22.     LinkedNode<T>* push_back(T value);
  23.     void set_head(LinkedNode<T>* node);
  24.     LinkedNode<T>* get_head();
  25.     void remove(LinkedNode<T>* node);
  26.    
  27.     template<typename IterF>
  28.     void iterate(IterF iterfunction);
  29.    
  30.     int length;
  31. };
  32.  
  33.  
  34. template<class T>
  35. LinkedList<T>::LinkedList()
  36. {
  37.     h_node = NULL;
  38.     length = 0;
  39. }
  40.  
  41. template<class T>
  42. LinkedList<T>::~LinkedList()
  43. {
  44.     LinkedNode<T>* conductor = h_node;
  45.     while(conductor->next != h_node)
  46.     {
  47.         conductor = conductor->next;
  48.         delete conductor;
  49.     }
  50.     delete h_node;
  51. }
  52.  
  53. template<class T>
  54. LinkedNode<T>* LinkedList<T>::push_back(T value)
  55. {
  56.     LinkedNode<T>* conductor = h_node;
  57.    
  58.  
  59.     if(h_node == NULL)
  60.     {
  61.         h_node = new LinkedNode<T>;
  62.         h_node->x = value;
  63.         h_node->next = h_node;
  64.         length++;
  65.         return h_node;
  66.     }
  67.    
  68.     while(conductor->next != h_node)
  69.     {
  70.         conductor = conductor->next;
  71.     }
  72.    
  73.     conductor->next = new LinkedNode<T>;
  74.     conductor->next->x = value;
  75.     conductor->next->next = h_node;
  76.     length++;
  77.     return conductor->next;
  78. }
  79.  
  80. template<class T>
  81. LinkedNode<T>* LinkedList<T>::push_front(T value)
  82. {
  83.     if(h_node == NULL)
  84.     {
  85.         h_node = new LinkedNode<T>;
  86.         h_node->x = value;
  87.         h_node->next = h_node;
  88.         length++;
  89.         return h_node;
  90.     }
  91.    
  92.     LinkedNode<T>* old_h = h_node;
  93.     h_node = new LinkedNode<T>;
  94.     h_node->x = value;
  95.     h_node->next = old_h;
  96.     length++;
  97.    
  98.     LinkedNode<T>* conductor = old_h;
  99.     while(conductor->next != old_h)
  100.     {
  101.         conductor = conductor->next;
  102.     }
  103.     conductor->next = h_node;
  104.     return h_node;
  105. }
  106.  
  107. template<class T>
  108. void LinkedList<T>::set_head(LinkedNode<T>* node)
  109. {
  110.     LinkedNode<T>* conductor = h_node;
  111.     int i=0;
  112.     while ( conductor->next != node )
  113.     {
  114.         conductor = conductor->next;
  115.  
  116.         if(i > length)
  117.             return;
  118.         i++;
  119.     }
  120.     h_node = node;
  121. }
  122.  
  123. template<class T>
  124. LinkedNode<T>* LinkedList<T>::get_head()
  125. {
  126.     return h_node;
  127. }
  128.  
  129. template<class T>
  130. void LinkedList<T>::remove(LinkedNode<T>* node)
  131. {
  132.     LinkedNode<T>* conductor = h_node;
  133.     LinkedNode<T>* prev_node;
  134.     int i=0;
  135.     while ( conductor->next != node )
  136.     {
  137.         prev_node = conductor;
  138.         conductor = conductor->next;
  139.  
  140.         if(i > length)
  141.             return;
  142.         i++;
  143.     }
  144.     // TODO: Why do an extra next. Why on earth does this work!?
  145.     prev_node->next->next = conductor->next->next;
  146.     if(node == h_node)
  147.     {
  148.         h_node = conductor->next->next;
  149.     }
  150.     delete node;
  151.     length--;
  152. }
  153.  
  154. template<class T>
  155. template<typename IterF>
  156. void LinkedList<T>::iterate(IterF iterfunction)
  157. {
  158.     LinkedNode<T>* conductor = h_node;
  159.    
  160.     iterfunction(h_node->x);
  161.     while ( conductor->next != h_node )
  162.     {
  163.         iterfunction(conductor->next->x);
  164.         conductor = conductor->next;
  165.     }
  166. }
  167.  
  168. int main()
  169. {
  170.     LinkedList<int>* LinkyLink = new LinkedList<int>();
  171.     LinkyLink->push_back(5);
  172.     LinkyLink->push_back(54);
  173.     LinkyLink->push_front(16);
  174.     LinkedNode<int>* A = LinkyLink->push_back(50);
  175.     LinkyLink->set_head(A);
  176.    
  177.     LinkyLink->iterate([&] (int x)
  178.     {
  179.         std::cout << x << std::endl;
  180.     });
  181.    
  182.     delete LinkyLink;
  183.     system("PAUSE");
  184.     return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement