Skip to content

Instantly share code, notes, and snippets.

@h0lyalg0rithm
Last active August 29, 2015 13:58
Show Gist options
  • Save h0lyalg0rithm/10365543 to your computer and use it in GitHub Desktop.
Save h0lyalg0rithm/10365543 to your computer and use it in GitHub Desktop.
Lists in Coffeescript
class List
constructor:()->
@dataStore=[]
@pos=0
@listSize = 0
length:()->
@listSize
clear:()->
@dataStore = []
@listSize = @pos = 0
toString:()->
@dataStore
insert:(element, prev)->
position = @find(prev)
if position > -1
@dataStore.splice(position, 0, element)
++@listSize
return true
return false
append:(element)->
@dataStore[++@listSize] = element
remove:(element)->
position = @find(element)
if position > -1
@dataStore.splice(position,1)
--@listSize
return true
return false
contains:(element)->
for elem in @dataStore
return true if elm == element
return false
front:()->
@pos = 0
end:()->
@pos = @listSize -1
prev:(func)->
--@pos if @pos > 0
next:(func)->
++@pos if @pos < @listSize - 1
currPos:(func)->
@pos
getElement:()->
@dataStore[@pos]
find:(element)->
for elem in @dataStore
return elem if elem == element
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment