forked from UNN/2026-rff_mp
100 lines
2.1 KiB
Python
100 lines
2.1 KiB
Python
import random
|
|
import time
|
|
import csv
|
|
import sys
|
|
|
|
sys.setrecursionlimit(20000)
|
|
|
|
def ll_insert(head, name, phone):
|
|
data = {'name': name, 'phone': phone, "next": None}
|
|
|
|
if head is None:
|
|
return data
|
|
|
|
current = head
|
|
while current:
|
|
if current['name'] == name:
|
|
current['phone'] = phone
|
|
return head
|
|
if current['next'] is None:
|
|
last = current
|
|
current = current['next']
|
|
|
|
last['next'] = data
|
|
return head
|
|
|
|
|
|
def ll_find(head, name):
|
|
current = head
|
|
while current:
|
|
if current['name'] == name:
|
|
return current['phone']
|
|
current = current['next']
|
|
return None
|
|
|
|
|
|
def ll_delete(head, name):
|
|
if head is None:
|
|
return None
|
|
|
|
if head['name'] == name:
|
|
return head['next']
|
|
|
|
prev = head
|
|
current = head['next']
|
|
while current:
|
|
if current['name'] == name:
|
|
prev['next'] = current['next']
|
|
return head
|
|
prev = current
|
|
current = current['next']
|
|
return head
|
|
|
|
|
|
def ll_list_all(head):
|
|
data_list = []
|
|
current = head
|
|
while current:
|
|
data_list.append({'name': current['name'], 'phone': current['phone']})
|
|
current = current['next']
|
|
data_list.sort(key=lambda x: x['name'])
|
|
return data_list
|
|
|
|
|
|
def hash_function(name, size):
|
|
return hash(name) % size
|
|
|
|
|
|
def ht_insert(buckets, name, phone):
|
|
index = hash_function(name, len(buckets))
|
|
head = buckets[index]
|
|
new_head = ll_insert(head, name, phone)
|
|
buckets[index] = new_head
|
|
return buckets
|
|
|
|
|
|
def ht_find(buckets, name):
|
|
index = hash_function(name, len(buckets))
|
|
head = buckets[index]
|
|
return ll_find(head, name)
|
|
|
|
|
|
def ht_delete(buckets, name):
|
|
index = hash_function(name, len(buckets))
|
|
head = buckets[index]
|
|
new_head = ll_delete(head, name)
|
|
buckets[index] = new_head
|
|
return buckets
|
|
|
|
|
|
def ht_list_all(buckets):
|
|
all_records = []
|
|
for head in buckets:
|
|
current = head
|
|
while current is not None:
|
|
all_records.append((current['name'], current['phone']))
|
|
current = current['next']
|
|
all_records.sort(key=lambda x: x[0])
|
|
return all_records
|
|
|