Skip to content

Instantly share code, notes, and snippets.

@isqad
Last active December 25, 2015 18:29
Show Gist options
  • Save isqad/7020446 to your computer and use it in GitHub Desktop.
Save isqad/7020446 to your computer and use it in GitHub Desktop.
# связанный список
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
@vkuznetsov
Copy link

А как заменить существующее значение? (return unless get(key).nil? не даёт)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment