diff --git a/data_structures/linked_list/sorted_linked_list.py b/data_structures/linked_list/sorted_linked_list.py index 60c240080..eb5ddda42 100644 --- a/data_structures/linked_list/sorted_linked_list.py +++ b/data_structures/linked_list/sorted_linked_list.py @@ -4,25 +4,27 @@ from collections.abc import Iterator from dataclasses import dataclass """ -This is a sorted linked list class that -creates a sorted linked list of integer datatype +This is a sorted linked list class that +creates a sorted linked list of integer datatype """ + @dataclass class Node: def __init__(self, data): self.data: int = data self.next_node: Node | None = None - + 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 - + def __repr__(self): nodes = [] temp = self.head @@ -30,12 +32,12 @@ class SortedLinedList: nodes.append(str(temp.data)) temp = temp.next_node return f"SortedLinkedList({', '.join(nodes)})" - + def insert(self, data: int): """This Function inserts node in it's sorted position - This function can be re written for any data type but + This function can be re written for any data type but the comparator her must have to be changed - + Args: data (int): the data of linked list """ @@ -55,21 +57,20 @@ class SortedLinedList: if new_node.next_node is None: self.tail = new_node 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: - """This Function deletes first appearance of node with - data from it's sorted position - - This function can be re written for any data type but + """This Function deletes first appearance of node with + data from it's sorted position + + This function can be re written for any data type but the comparator her must have to be changed Args: @@ -97,9 +98,9 @@ class SortedLinedList: temp_node = temp_node.next_node return False - + def search(self, data: int) -> bool: - """This function searches the data given input from user + """This function searches the data given input from user and return whether the data exists or not Args: @@ -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 @@ -121,8 +123,8 @@ class SortedLinedList: bool: flag indicating whether list is empty or not """ return self.head is None - - def length (self) -> int: + + def length(self) -> int: """This function returns the length of the linked list @@ -130,7 +132,7 @@ class SortedLinedList: int: The length of linked list """ return numNodes - + def min_value(self) -> int | None: """This function will return minimum value @@ -140,7 +142,7 @@ class SortedLinedList: if self.head is None: return None return self.head.data - + def max_value(self) -> int | None: """This function will return maximum value @@ -151,20 +153,18 @@ class SortedLinedList: if self.tail is None: return None 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: temp.next_node = temp.next_node.next_node else: 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: @@ -188,36 +188,30 @@ class SortedLinedList: return self.tail.next_node = other_list.head self.tail = other_list.tail - + 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: ") - - if choice == '1': + choice = input("Enter your choice: ") + + 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") - - - - - - \ No newline at end of file