Evyatar12

LinkedListUtils

Nov 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Nodes
  8. {
  9. class LinkedListUtils
  10. {
  11. public static Node<T> createList<T>(T[] valuesArray)
  12. {
  13. Node<T> current = null;
  14.  
  15. for (int i = valuesArray.Length - 1; i >= 0; i--)
  16. {
  17. if (current != null)
  18. current = new Node<T>(valuesArray[i], current);
  19. else
  20. current = new Node<T>(valuesArray[i]);
  21. }
  22.  
  23. return current;
  24. }
  25.  
  26.  
  27. public static Node<T> leaveOnly<T>(Node<T> pos, Predicate<Node<T>> condition)
  28. {
  29.  
  30. Node<T> first = null,
  31. lastValid = null;
  32.  
  33. while (pos != null)
  34. {
  35. if (condition(pos)) // if the elements meets the condition
  36. {
  37. if (first == null) // if first element was not set yet, set it to the one found now
  38. first = pos;
  39. else // if a first element was set already, add pointer to last element
  40. lastValid.setNext(pos);
  41.  
  42. lastValid = pos;
  43. }
  44.  
  45. pos = pos.getNext(); // move to next element
  46. }
  47.  
  48. return first;
  49. }
  50.  
  51. public static Node<T> remove<T>(Node<T> pos, Predicate<Node<T>> condition)
  52. {
  53. return leaveOnly<T>(pos,
  54. node => !condition(node) // lambda expression for making a 'not' predicate
  55. );
  56. }
  57.  
  58. public static string toString<T>(Node<T> pos)
  59. {
  60. string list = "[";
  61.  
  62. while (pos != null)
  63. {
  64. // add comma if there is more than one element
  65. if (list.Length != 1)
  66. list += ", ";
  67.  
  68. // add value to string
  69. list += pos.getValue();
  70.  
  71. // move to next element
  72. pos = pos.getNext();
  73. }
  74.  
  75. list += "]"; // close list
  76.  
  77. return list;
  78. }
  79.  
  80. public static void print<T>(Node<T> pos)
  81. {
  82. Console.Write(toString<T>(pos));
  83. }
  84.  
  85. public static void println<T>(Node<T> pos)
  86. {
  87. Console.WriteLine(toString<T>(pos));
  88. }
  89.  
  90. public static Node<int> createNumbersList(int start, int end, int step = 1)
  91. {
  92. if ((end - start) * step < 0) // if there's no way to reach the end with the step given
  93. return null;
  94.  
  95. // initialize variables
  96. Node<int> first = null, // first element of the list
  97. pos = null; // the current position
  98.  
  99. int current = start;
  100.  
  101.  
  102. // while steps have not reached the destination
  103. while (
  104. (end >= start && current <= end) ||
  105. start > end && current >= end
  106. )
  107. {
  108. if (first == null) // set first element if needed
  109. {
  110. first = new Node<int>(current);
  111. pos = first;
  112. }
  113. else
  114. {
  115. pos.setNext(new Node<int>(current));
  116. pos = pos.getNext();
  117. }
  118.  
  119. current += step;
  120. }
  121.  
  122. return first;
  123. }
  124.  
  125. public static int listLength<T>(Node<T> list)
  126. {
  127. int counter = 0;
  128.  
  129. Node<T> pos = list;
  130.  
  131. while (pos != null)
  132. {
  133. counter++;
  134. pos = pos.getNext();
  135. }
  136.  
  137. return counter;
  138. }
  139. }
  140.  
  141. }
Add Comment
Please, Sign In to add comment