From e4a4cae41879a5e9f435396c4b584d0f34e05daa Mon Sep 17 00:00:00 2001 From: Donald Calloway Date: Thu, 9 Oct 2025 08:44:44 -0700 Subject: [PATCH] Added doubly_lined_class.py module and list. --- doubly_linked_class.py | 80 +++++++++++++++++++++++++++ doubly_linked_list.py | 120 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 doubly_linked_class.py create mode 100644 doubly_linked_list.py diff --git a/doubly_linked_class.py b/doubly_linked_class.py new file mode 100644 index 0000000..e9722ae --- /dev/null +++ b/doubly_linked_class.py @@ -0,0 +1,80 @@ +class Node: + def __init__(self, data: int | float | list | dict | tuple | set): + self.data = data + self.next = None + self.prev = None + +class DoublyLinkedList: + def __init__(self): + self.head = None + + def append(self, data): + """Add a node with the given data to the end of the list.""" + new_node = Node(data) + if not self.head: + self.head = new_node + return + curr = self.head + while curr.next: + curr = curr.next + curr.next = new_node + new_node.prev = curr + + def prepend(self, data): + """Add a node with the given data to the beginning of the list.""" + new_node = Node(data) + if self.head: + self.head.prev = new_node + new_node.next = self.head + self.head = new_node + + def insert_at_position(self, position, data): + """Insert a node with the given data at the specified position (0-based).""" + if position <= 0 or not self.head: + self.prepend(data) + return + new_node = Node(data) + curr = self.head + index = 0 + while curr.next and index < position - 1: + curr = curr.next + index += 1 + new_node.next = curr.next + new_node.prev = curr + if curr.next: + curr.next.prev = new_node + curr.next = new_node + + def delete_value(self, value): + """Remove the first node that contains the specified value.""" + curr = self.head + while curr: + if curr.data == value: + if curr.prev: + curr.prev.next = curr.next + else: + self.head = curr.next + if curr.next: + curr.next.prev = curr.prev + return True + curr = curr.next + return False + + def search(self, value): + """Return True if a node with the specified value exists, otherwise False.""" + curr = self.head + while curr: + if curr.data == value: + return True + curr = curr.next + return False + + def print_list(self): + """Print all elements in the list in order.""" + elems = [] + curr = self.head + while curr: + elems.append(str(curr.data)) + curr = curr.next + print(" <-> ".join(elems)) + \ No newline at end of file diff --git a/doubly_linked_list.py b/doubly_linked_list.py new file mode 100644 index 0000000..3f3cf02 --- /dev/null +++ b/doubly_linked_list.py @@ -0,0 +1,120 @@ +class Node: + def __init__(self, data: int | float | dict | tuple | set): + self.data = data + self.next = None + self.prev = None + +class LinkedList: + def __init__(self): + self.head = None + + def append(self, data): + """Add a node with the given data to the end of the list.""" + new_node = Node(data) + if not self.head: + self.head = new_node + return + curr = self.head + while curr.next: + curr = curr.next + curr.next = new_node + new_node.prev = curr + + def prepend(self, data): + """Add a node with the given data to the beginning of the list.""" + new_node = Node(data) + if self.head: + self.head.prev = new_node + new_node.next = self.head + self.head = new_node + + def insert_at_position(self, position, data): + """Insert a node with the given data at the specified position (0-based).""" + if position <= 0 or not self.head: + self.prepend(data) + return + new_node = Node(data) + curr = self.head + index = 0 + while curr.next and index < position - 1: + curr = curr.next + index += 1 + new_node.next = curr.next + new_node.prev = curr + if curr.next: + curr.next.prev = new_node + curr.next = new_node + + def delete_value(self, value): + """Remove the first node that contains the specified value.""" + curr = self.head + while curr: + if curr.data == value: + if curr.prev: + curr.prev.next = curr.next + else: + self.head = curr.next + if curr.next: + curr.next.prev = curr.prev + return True + curr = curr.next + return False + + def search(self, value): + """Return True if a node with the specified value exists, otherwise False.""" + curr = self.head + while curr: + if curr.data == value: + return True + curr = curr.next + return False + + def print_list(self): + """Print all elements in the list in order.""" + elems = [] + curr = self.head + while curr: + elems.append(str(curr.data)) + curr = curr.next + print(" <-> ".join(elems)) + + +def build_dbl_linked_list_range(start, end, step): + dll = LinkedList() + for elem in range(start, end, step): + dll.append(elem) + dll.print_list() + return + + + + +# Example usage +if __name__ == "__main__": + dll = LinkedList() + dll.append(10) + dll.append(20) + dll.prepend(5) + dll.insert_at_position(1, 7) + dll.print_list() # 5 <-> 7 <-> 10 <-> 20 + print(dll.search(10)) # True + print(dll.search(100)) # False + dll.delete_value(7) + dll.print_list() # 5 <-> 10 <-> 20 + dll.delete_value(5) + dll.print_list() # 10 <-> 20 + dll.insert_at_position(1, 15) + dll.print_list() # 10 <-> 15 <-> 20 + print(dll.search(15)) # True + dll.append({'apple': 4}) + dll.append({'beet': 7}) + dll.append({'corn': 5}) + dll.append({'grape': 9}) + dll.append((4, 9)) + dll.append(('hello', 'world')) + dll.append({5, 8, 1, 8, 10}) # Only unique values appear in the appended set -> {8, 1, 10, 5} + dll.print_list() + + build_dbl_linked_list_range(2, 21, 2) + + \ No newline at end of file