Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. {LinkedList<T> head:><T>- tail size:int}
  2. {Node<T> value:<T> next:><T>}
  3.  
  4. ? Get element at index
  5. >Node<T> [get list:LinkedList<T> index:int
  6. (ifnot (outofbounds list index)
  7. (set node:Node<T> list.head)
  8. (for (set i:int) (< i index) (++ i)
  9. (set node node.next))
  10. (ret node)
  11. (else
  12. (throw IndexOutOfBounds
  13. (fstr "{0} is out of bounds. The current size of the list is {1}" {index list.size}))))
  14. ]
  15.  
  16. ? Check if index is out of bounds
  17. bool [outofbounds list:LinkedList<T> index:int
  18. (if (> index list.size)
  19. (ret true)
  20. (else
  21. (ret false))
  22. ]
  23.  
  24. ? Insert value at end
  25. [insert list:LinkedList<T> node:Node<T>
  26. (set list.tail.next node)
  27. (set list.tail node)
  28. ]
  29.  
  30. ? Insert value at index
  31. [insertat list:LinkedList<T> node:Node<T> index:int
  32. (ifnot (outofbounds index list.size)
  33. (set (retpush (get list (- index))).next node)
  34. (set node.next (pop Node<T>).next)
  35. (else
  36. (throw IndexOutOfBounds
  37. (fstr "{0} is out of bounds. The current size of the list is {1}" {index list.size}))))
  38. ]
  39.  
  40. ? Remove value at index
  41. [removeat list:LinkedList<T> index:int
  42. (ifnot (outofbounds index list.size)
  43. (set previous:Node<T> (get list (- index)))
  44. (set toremove:Node<T> previous.next)
  45. (set new:Node<T> toremove.next)
  46. (del toremove)
  47. (set previous.next new)
  48. (else
  49. (throw IndexOutOfBounds
  50. (fstr "{0} is out of bounds. The current size of the list is {1}" {index list.size}))))
  51. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement