Skip to content

Instantly share code, notes, and snippets.

@OfTheDelmer
Last active December 29, 2015 23:49
Show Gist options
  • Save OfTheDelmer/7745196 to your computer and use it in GitHub Desktop.
Save OfTheDelmer/7745196 to your computer and use it in GitHub Desktop.
mixing hashes with idea of linked lists
####
# first->next list
####
class HashyList
def initialize(*args)
@first=nil
@next=nil
@items = {}
args != [] ? digest(args) : @items[@first] = @next
end
def insertAfter(item, newItem)
if( @items[item] )
oldItem = @items[item]
@items[item] = newItem
@items[newItem] = oldItem
end
end
def insertFirst(item)
@items[item] = @first
@first = item
end
def first
@first
end
def next
@items[@next]
end
private
def digest(items)
if(items != [])
@next = @first
@first = items.pop()
@items[@first] = @next
digest(items)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment