Last active
December 25, 2015 18:29
-
-
Save isqad/7020446 to your computer and use it in GitHub Desktop.
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 SeqList | |
attr_accessor :first | |
class Node | |
attr_accessor :key, :val, :next_node | |
def initialize(key, val, next_node) | |
@key, @val, @next_node = key, val, next_node | |
end | |
end | |
def get(key) | |
x = @first | |
while !x.nil? do | |
return x.val if key.eql?(x.key) | |
x = x.next_node | |
end | |
end | |
def put(key, val) | |
node = get(key) | |
node.val = val and return node unless node.nil? | |
@first = Node.new(key, val, first) | |
end | |
end | |
class HashTable | |
attr_reader :ht | |
def initialize | |
@ht = [] | |
end | |
def get_hash(key) | |
# здесь я рассчитываю максимум на 7 ключей | |
key.hash % 7 | |
end | |
def get(key) | |
return if @ht[get_hash(key)].nil? | |
@ht[get_hash(key)].get(key) | |
end | |
def put(key, val) | |
@ht[get_hash(key)] = SeqList.new if @ht[get_hash(key)].nil? | |
@ht[get_hash(key)].put(key, val) | |
end | |
private :get_hash | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
А как заменить существующее значение? (return unless get(key).nil? не даёт)