Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. template <class T>
  2. List<T> List<T>::split(int splitPoint)
  3. {
  4. if (splitPoint > length)
  5. return List<T>();
  6.  
  7. if (splitPoint < 0)
  8. splitPoint = 0;
  9.  
  10. ListNode * secondHead = split(head, splitPoint);
  11.  
  12. int oldLength = length;
  13. if (secondHead == head)
  14. {
  15. // current list is going to be empty
  16. head = NULL;
  17. tail = NULL;
  18. length = 0;
  19. }
  20. else
  21. {
  22. // set up current list
  23. tail = head;
  24. while (tail->next != NULL)
  25. tail = tail->next;
  26. length = splitPoint;
  27. }
  28.  
  29. // set up the returned list
  30. List<T> ret;
  31. ret.head = secondHead;
  32. ret.tail = secondHead;
  33. if (ret.tail != NULL)
  34. {
  35. while (ret.tail->next != NULL)
  36. ret.tail = ret.tail->next;
  37. }
  38. ret.length = oldLength - splitPoint;
  39. return ret;
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement