Skip to content

Instantly share code, notes, and snippets.

@aeharding
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeharding/9846467 to your computer and use it in GitHub Desktop.
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!
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