Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. class Node
  2. attr_accessor :value, :next_node
  3. def initialize(value)
  4. @value = value
  5. @next_node = nil
  6. end
  7. def tail?
  8. @next_node.nil?
  9. end
  10. end
  11.  
  12. class LinkedList
  13. attr_accessor :head
  14. def initialize
  15. @head = nil
  16. end
  17.  
  18. def add_to_list(value)
  19. node = @head
  20. if @head.nil?
  21. @head = Node.new value
  22. else
  23. while node.next_node
  24. node = node.next_node
  25. end
  26. node.next_node = Node.new value
  27.  
  28. end
  29.  
  30. end
  31.  
  32. def find(value)
  33. node = @head
  34. if @head == nil
  35. return false
  36. end
  37. if @head.value == value
  38. return @head
  39. end
  40.  
  41. while node = node.next_node
  42. if node.value == value
  43. return node
  44. end
  45.  
  46. end
  47.  
  48. return false
  49.  
  50. end
  51.  
  52. def reverset
  53. previous = nil
  54. next_node = nil
  55. current = @head
  56. while current
  57. # save the ref to next node to move forward later
  58. next_node = current.next_node
  59. # break current link, and set it to node before
  60. current.next_node = previous
  61. #keep traversing the list by moving pointers forward
  62. previous = current
  63. current = next_node
  64.  
  65. end
  66. @head = previous
  67.  
  68.  
  69.  
  70. end
  71.  
  72. def prependd(value)
  73. node = Node.new value
  74. old = @head
  75. @head = node
  76. @head.next_node= old
  77.  
  78. end
  79.  
  80. def deleteat(value)
  81. node = @head
  82. if @head == nil
  83. return
  84. end
  85. if @head.value == value
  86. @head = @head.next_node
  87. return
  88. end
  89.  
  90.  
  91. while node.next_node
  92. if node.next_node.value == value
  93. node.next_node = node.next_node.next_node
  94. return
  95. end
  96.  
  97. end
  98. end
  99.  
  100. def insert_after(value, target)
  101. node = @head
  102. if @head.value == target
  103. old = node.next_node
  104. node.next_node = Node.new value
  105. node.next_node.next_node = old
  106. return
  107. end
  108. while node.next_node
  109. if node.value == target
  110. old = node.next_node
  111. node.next_node = Node.new value
  112. node.next_node.next_node = old
  113. return
  114. end
  115. end
  116. end
  117.  
  118. def count
  119. node = @head
  120. if @head == nil
  121. return 0
  122. else
  123. count = 1
  124. while node.next_node
  125. count+=1
  126.  
  127. end
  128. end
  129. count
  130.  
  131.  
  132.  
  133. end
  134.  
  135. def to_string
  136. node = @head
  137. return "" if @head.nil?
  138. while node.next_node
  139. p "The #{head.value} family"
  140. end
  141. end
  142. def last_node(node)
  143. return node if node.tail?
  144. last_node(node.next_node)
  145. end
  146.  
  147. end
  148.  
  149.  
  150.  
  151. linkedlist = LinkedList.new()
  152. linkedlist.add_to_list(100)
  153. linkedlist.add_to_list(4)
  154. linkedlist.add_to_list(6)
  155. linkedlist.add_to_list(8)
  156.  
  157. p linkedlist
  158.  
  159. linkedlist.find(4)
  160. linkedlist.prependd(0)
  161. # p linkedlist
  162.  
  163. # linkedlist.deleteat(100)
  164. # p linkedlist
  165. # linkedlist.insert_after(250,4)
  166. #linkedlist.insert_after(500,0)
  167. p linkedlist
  168. p "reverse"
  169. #p reverse_list(linkedlist)
  170. p linkedlist.reverset
  171.  
  172. # p linkedlist.find_value(5)
  173. # p linkedlist.find_value(6)
  174. # p linkedlist.find_value(8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement