Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 668c233d2c | |||
| 8a5904376c | |||
| 6916bf235c |
119
BudakovIS/docs/LinkedListPhoneBook.py
Normal file
119
BudakovIS/docs/LinkedListPhoneBook.py
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
head = None
|
||||
|
||||
#node1 = {'name' : 'Ivan', 'phone' : '123-456', 'next' : None}
|
||||
#head = node1
|
||||
|
||||
#node2 = {'name' : 'Dima', 'phone' : '789-123', 'next' : None}
|
||||
#node1['next'] = node2
|
||||
|
||||
def ll_insert(head, name, phone):
|
||||
|
||||
curent = head
|
||||
while curent is not None:
|
||||
if curent['name'] == name:
|
||||
curent['phone'] = phone
|
||||
return head
|
||||
curent = curent['next']
|
||||
|
||||
|
||||
n_node = {'name' : name, 'phone' : phone, 'next' : None}
|
||||
|
||||
if head is None:
|
||||
return n_node
|
||||
|
||||
curent = head
|
||||
while curent['next'] is not None:
|
||||
curent = curent['next']
|
||||
curent['next'] = n_node
|
||||
return head
|
||||
|
||||
|
||||
|
||||
print("====== TESTING ll_insert FUNC ========")
|
||||
head = ll_insert(head,'Ivan','123-456')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Boris', '123-456')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Ivan', '321-654')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Dima', '345-678')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Boris', '111-222')
|
||||
|
||||
print(head)
|
||||
|
||||
head = ll_insert(head, 'Methody', '221-112')
|
||||
|
||||
head = ll_insert(head, 'Kiril', '112-221')
|
||||
|
||||
print(f"======= END TEST =======\n\n\n")
|
||||
|
||||
|
||||
def ll_find(head, name):
|
||||
curent = head
|
||||
while curent is not None:
|
||||
if curent['name'] == name:
|
||||
return curent['phone']
|
||||
curent = curent['next']
|
||||
return None
|
||||
|
||||
print("====== TESTING ll_find FUNC ======")
|
||||
|
||||
print("Ivan`s phone: "+ ll_find(head, 'Ivan'))
|
||||
|
||||
print("Dima`s phone: "+ ll_find(head, 'Dima'))
|
||||
|
||||
print("Boris phone: "+ ll_find(head, 'Boris'))
|
||||
|
||||
print(f"====== END TEST ======\n\n\n")
|
||||
|
||||
|
||||
def ll_delete(head, name):
|
||||
if head is None:
|
||||
return None
|
||||
|
||||
if head['name'] == name:
|
||||
return head['next']
|
||||
|
||||
prev = head
|
||||
curent = head['next']
|
||||
while curent is not None:
|
||||
if curent['name'] == name:
|
||||
prev['next'] = curent['next']
|
||||
return head
|
||||
prev = curent
|
||||
curent = curent['next']
|
||||
return head
|
||||
|
||||
|
||||
print("====== TEST ll_delete FUNC ======")
|
||||
|
||||
print("Del of Dima:", ll_delete(head, 'Dima'))
|
||||
|
||||
print("====== END TEST ======")
|
||||
|
||||
|
||||
def ll_list_all(head):
|
||||
records = []
|
||||
curent = head
|
||||
while curent is not None:
|
||||
records.append((curent['name'],curent['phone']))
|
||||
curent = curent['next']
|
||||
records.sort(key=lambda pair: pair[0])
|
||||
return records
|
||||
|
||||
print(f"\n\n\n\n")
|
||||
|
||||
print("====== TESTING ll_list_all FUNC ======")
|
||||
|
||||
print(ll_list_all(head))
|
||||
|
||||
print("====== END ======")
|
||||
Loading…
Reference in New Issue
Block a user