Last active
December 29, 2015 23:49
-
-
Save OfTheDelmer/7745196 to your computer and use it in GitHub Desktop.
mixing hashes with idea of linked lists
This file contains hidden or 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
| #### | |
| # 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