mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-16 10:47:37 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
0a1c71e34d
commit
583e564987
@ -8,6 +8,7 @@ This is a sorted linked list class that
|
||||
creates a sorted linked list of integer datatype
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class Node:
|
||||
def __init__(self, data):
|
||||
@ -17,9 +18,10 @@ class Node:
|
||||
def __repr__(self):
|
||||
return f"Node({self.data}, {self.next_node})"
|
||||
|
||||
|
||||
class SortedLinedList:
|
||||
def __init__(self):
|
||||
self.numNodes : int = 0
|
||||
self.numNodes: int = 0
|
||||
self.head: Node | None = None
|
||||
self.tail: Node | None = None
|
||||
|
||||
@ -57,12 +59,11 @@ class SortedLinedList:
|
||||
self.numNodes += 1
|
||||
|
||||
def display(self):
|
||||
"""This function displays whole list
|
||||
"""
|
||||
temp=self.head
|
||||
"""This function displays whole list"""
|
||||
temp = self.head
|
||||
while temp:
|
||||
print(temp.data,end=" ")
|
||||
temp=temp.next_node
|
||||
print(temp.data, end=" ")
|
||||
temp = temp.next_node
|
||||
print()
|
||||
|
||||
def delete(self, data: int) -> bool:
|
||||
@ -114,6 +115,7 @@ class SortedLinedList:
|
||||
return True
|
||||
temp = temp.next_node
|
||||
return False
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""This function will check whether the list is empty or not
|
||||
|
||||
@ -122,7 +124,7 @@ class SortedLinedList:
|
||||
"""
|
||||
return self.head is None
|
||||
|
||||
def length (self) -> int:
|
||||
def length(self) -> int:
|
||||
"""This function returns the length of the linked list
|
||||
|
||||
|
||||
@ -153,8 +155,7 @@ class SortedLinedList:
|
||||
return self.tail.data
|
||||
|
||||
def remove_duplicates(self):
|
||||
"""This Function will remove the duplicates from the list
|
||||
"""
|
||||
"""This Function will remove the duplicates from the list"""
|
||||
temp = self.head
|
||||
while temp and temp.next_node:
|
||||
if temp.data == temp.next_node.data:
|
||||
@ -163,8 +164,7 @@ class SortedLinedList:
|
||||
temp = temp.next_node
|
||||
|
||||
def reverse(self):
|
||||
"""This function will reveres the list
|
||||
"""
|
||||
"""This function will reveres the list"""
|
||||
prev = None
|
||||
current = self.head
|
||||
while current:
|
||||
@ -191,33 +191,27 @@ class SortedLinedList:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
linkedList=SortedLinedList()
|
||||
linkedList = SortedLinedList()
|
||||
while True:
|
||||
print("Enter")
|
||||
print("1. Insert")
|
||||
print("2. Display")
|
||||
print("3. Delete")
|
||||
print("4. Exit")
|
||||
choice= input("Enter your choice: ")
|
||||
choice = input("Enter your choice: ")
|
||||
|
||||
if choice == '1':
|
||||
if choice == "1":
|
||||
data = int(input("Enter a number: "))
|
||||
linkedList.insert(data)
|
||||
elif choice == '2':
|
||||
elif choice == "2":
|
||||
linkedList.display()
|
||||
elif choice == '3':
|
||||
elif choice == "3":
|
||||
data = int(input("Enter the data to delete: "))
|
||||
if linkedList.delete(data):
|
||||
print("Node with data {} deleted successfully".format(data))
|
||||
else:
|
||||
print("Node with data {} not found in the list".format(data))
|
||||
elif choice == '4':
|
||||
elif choice == "4":
|
||||
break
|
||||
else:
|
||||
print("Wrong input")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user