Added doubly_lined_class.py module and list.

This commit is contained in:
Donald Calloway 2025-10-09 08:44:44 -07:00
parent b122a870cf
commit e4a4cae418
2 changed files with 200 additions and 0 deletions

80
doubly_linked_class.py Normal file
View File

@ -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))

120
doubly_linked_list.py Normal file
View File

@ -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)