Last active
August 29, 2015 13:57
-
-
Save aeharding/9846467 to your computer and use it in GitHub Desktop.
A LinkedList (with Node and Iterator) written in Coffeescript to exercise my mind!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedList | |
isEmpty: -> | |
not @head? | |
prepend: (item) -> | |
node = new Node item, @head | |
@tail = node if @isEmpty() | |
@head = node | |
append: (item) -> | |
node = new Node item, null | |
if @tail? | |
@tail.next = node | |
if @isEmpty() | |
@head = node | |
@tail = node | |
iterator: -> | |
new Iterator @head | |
class Iterator | |
constructor: (@pos) -> | |
hasNext: -> | |
@pos? | |
next: -> | |
throw new Error "Dafuq?" if not @hasNext() | |
ret = @pos | |
@pos = @pos.next | |
ret.item | |
class Node | |
constructor: (@item, @next) -> | |
blah = new LinkedList() | |
blah.append "LOL" | |
blah.append "JK" | |
blah.prepend "Whee!" | |
blah.prepend 4 | |
blah.prepend "WHAT IS HAPPENING." | |
itr = blah.iterator() | |
while itr.hasNext() | |
console.log itr.next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment