Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {LinkedList<T> head:><T>- tail size:int}
- {Node<T> value:<T> next:><T>}
- ? Get element at index
- >Node<T> [get list:LinkedList<T> index:int
- (ifnot (outofbounds list index)
- (set node:Node<T> list.head)
- (for (set i:int) (< i index) (++ i)
- (set node node.next))
- (ret node)
- (else
- (throw IndexOutOfBounds
- (fstr "{0} is out of bounds. The current size of the list is {1}" {index list.size}))))
- ]
- ? Check if index is out of bounds
- bool [outofbounds list:LinkedList<T> index:int
- (if (> index list.size)
- (ret true)
- (else
- (ret false))
- ]
- ? Insert value at end
- [insert list:LinkedList<T> node:Node<T>
- (set list.tail.next node)
- (set list.tail node)
- ]
- ? Insert value at index
- [insertat list:LinkedList<T> node:Node<T> index:int
- (ifnot (outofbounds index list.size)
- (set (retpush (get list (- index))).next node)
- (set node.next (pop Node<T>).next)
- (else
- (throw IndexOutOfBounds
- (fstr "{0} is out of bounds. The current size of the list is {1}" {index list.size}))))
- ]
- ? Remove value at index
- [removeat list:LinkedList<T> index:int
- (ifnot (outofbounds index list.size)
- (set previous:Node<T> (get list (- index)))
- (set toremove:Node<T> previous.next)
- (set new:Node<T> toremove.next)
- (del toremove)
- (set previous.next new)
- (else
- (throw IndexOutOfBounds
- (fstr "{0} is out of bounds. The current size of the list is {1}" {index list.size}))))
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement