Applied Non safety

This commit is contained in:
mjk22071998 2024-10-01 16:07:48 +05:00
parent 3c76fcef53
commit 44ebb6632b

View File

@ -93,12 +93,13 @@ class SortedLinkedList:
self.head = new_node self.head = new_node
else: else:
temp_node: Node | None = self.head temp_node: Node | None = self.head
while temp_node.next_node and temp_node.next_node.data < data: if temp:
temp_node = temp_node.next_node while temp_node.next_node and temp_node.next_node.data < data:
new_node.next_node = temp_node.next_node temp_node = temp_node.next_node
temp_node.next_node = new_node new_node.next_node = temp_node.next_node
if new_node.next_node is None: temp_node.next_node = new_node
self.tail = new_node if new_node.next_node is None:
self.tail = new_node
self.numNodes += 1 self.numNodes += 1
def display(self) -> None: def display(self) -> None:
@ -156,13 +157,14 @@ class SortedLinkedList:
return True return True
temp_node: Node | None = self.head temp_node: Node | None = self.head
while temp_node.next_node: if temp_node:
if temp_node.next_node.data == data: while temp_node.next_node:
temp_node.next_node = temp_node.next_node.next_node if temp_node.next_node.data == data:
if temp_node.next_node is None: temp_node.next_node = temp_node.next_node.next_node
self.tail = temp_node if temp_node.next_node is None:
return True self.tail = temp_node
temp_node = temp_node.next_node return True
temp_node = temp_node.next_node
return False return False
@ -363,4 +365,4 @@ if __name__ == "__main__":
elif choice == "4": elif choice == "4":
break break
else: else:
print("Wrong input") print("Wrong input")