Связный список 2.0(прошлый был тестовый)

This commit is contained in:
Alexander 2026-05-24 23:21:43 +03:00
parent 52896e6f1d
commit 3cba71c6e7

View File

@ -1,22 +1,22 @@
import time
import random
import csv
import matplotlib.pyplot as plt
import numpy as np
def ll_insert(head, name, phone):
"""Вставка в конец списка. Возвращает голову."""
new_node = {'name': name, 'phone': phone, 'next': None}
if head is None:
return new_node
current = head
while current['next']:
while current:
if current['name'] == name:
current['phone'] = phone
return head
if current['next'] is None:
current['next'] = new_node
return head
current = current['next']
if current['name'] == name:
current['phone'] = phone
else:
current['next'] = new_node
return head
def ll_find(head, name):
@ -30,24 +30,20 @@ def ll_find(head, name):
def ll_delete(head, name):
if head is None:
return None
if head['name'] == name:
return head['next']
current = head
while current['next']:
if current['next']['name'] == name:
current['next'] = current['next']['next']
return head
current = current['next']
return head
def ll_list_all(head):
entries = []
records = []
current = head
while current:
entries.append((current['name'], current['phone']))
records.append((current['name'], current['phone']))
current = current['next']
entries.sort(key=lambda x: x[0])
return entries
return sorted(records, key=lambda x: x[0])