Tutorial 1 - 7 February 2025
This commit is contained in:
parent
8cf111ec7a
commit
60ad042acc
|
@ -0,0 +1,102 @@
|
|||
class ListNode:
|
||||
def __init__(self, item):
|
||||
self.item = item
|
||||
self.next = None
|
||||
|
||||
class LinkedList:
|
||||
def __init__(self):
|
||||
self.size = 0
|
||||
self.head = None
|
||||
|
||||
def printList(self):
|
||||
cur = self.head
|
||||
if not cur:
|
||||
print("Empty")
|
||||
return
|
||||
while cur:
|
||||
print(cur.item, end=" ")
|
||||
cur = cur.next
|
||||
print()
|
||||
|
||||
def removeAllItems(self):
|
||||
self.head = None
|
||||
self.size = 0
|
||||
|
||||
def findNode(self, index):
|
||||
if index < 0 or index >= self.size:
|
||||
return None
|
||||
cur = self.head
|
||||
while index > 0:
|
||||
cur = cur.next
|
||||
index -= 1
|
||||
return cur
|
||||
|
||||
def insertNode(self, index, value):
|
||||
if index < 0 or index > self.size:
|
||||
return -1
|
||||
|
||||
newNode = ListNode(value)
|
||||
if index == 0: # Insert at the beginning
|
||||
newNode.next = self.head
|
||||
self.head = newNode
|
||||
else:
|
||||
prev = self.findNode(index - 1)
|
||||
newNode.next = prev.next
|
||||
prev.next = newNode
|
||||
|
||||
self.size += 1
|
||||
return 0
|
||||
|
||||
def removeNode(self, index):
|
||||
if index < 0 or index >= self.size:
|
||||
return -1
|
||||
|
||||
if index == 0: # Remove the first node
|
||||
self.head = self.head.next
|
||||
else:
|
||||
prev = self.findNode(index - 1)
|
||||
prev.next = prev.next.next
|
||||
|
||||
self.size -= 1
|
||||
return 0
|
||||
|
||||
def moveEvenItemsToBack(ll):
|
||||
if ll.size < 2:
|
||||
return
|
||||
|
||||
cur = ll.head
|
||||
index = 0
|
||||
for _ in range(ll.size):
|
||||
if cur.item % 2 == 0:
|
||||
even_value = cur.item
|
||||
cur = cur.next
|
||||
ll.removeNode(index)
|
||||
ll.insertNode(ll.size, even_value)
|
||||
else:
|
||||
cur = cur.next
|
||||
index += 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
ll = LinkedList()
|
||||
|
||||
while True:
|
||||
print("1: Insert an integer to the linked list:")
|
||||
print("2: Move all even integers to the back of the linked list:")
|
||||
print("0: Quit:")
|
||||
|
||||
choice = int(input("Please input your choice (1/2/0): "))
|
||||
|
||||
if choice == 1:
|
||||
value = int(input("Input an integer that you want to add to the linked list: "))
|
||||
ll.insertNode(ll.size, value)
|
||||
print("The resulting linked list is:")
|
||||
ll.printList()
|
||||
elif choice == 2:
|
||||
moveEvenItemsToBack(ll)
|
||||
print("The resulting linked list after moving even integers to the back is:")
|
||||
ll.printList()
|
||||
elif choice == 0:
|
||||
ll.removeAllItems()
|
||||
break
|
||||
else:
|
||||
print("Choice unknown;")
|
|
@ -0,0 +1,119 @@
|
|||
class ListNode:
|
||||
def __init__(self, item):
|
||||
self.item = item
|
||||
self.next = None
|
||||
|
||||
class LinkedList:
|
||||
def __init__(self):
|
||||
self.size = 0
|
||||
self.head = None
|
||||
|
||||
def printList(ll):
|
||||
cur = ll.head
|
||||
if cur is None:
|
||||
print("Empty")
|
||||
return
|
||||
while cur is not None:
|
||||
print(cur.item, end=" ")
|
||||
cur = cur.next
|
||||
print()
|
||||
|
||||
def findNode(ll, index):
|
||||
if ll is None or index < 0 or index >= ll.size:
|
||||
return None
|
||||
|
||||
temp = ll.head
|
||||
while index > 0:
|
||||
temp = temp.next
|
||||
if temp is None:
|
||||
return None
|
||||
index -= 1
|
||||
|
||||
return temp
|
||||
|
||||
def insertNode(ll, index, value):
|
||||
if ll is None or index < 0 or index > ll.size:
|
||||
return -1
|
||||
|
||||
new_node = ListNode(value)
|
||||
|
||||
if index == 0: # Insert at the beginning
|
||||
new_node.next = ll.head
|
||||
ll.head = new_node
|
||||
else:
|
||||
pre = findNode(ll, index - 1)
|
||||
if pre is None:
|
||||
return -1
|
||||
new_node.next = pre.next
|
||||
pre.next = new_node
|
||||
|
||||
ll.size += 1
|
||||
return 0
|
||||
|
||||
def removeNode(ll, index):
|
||||
if ll is None or index < 0 or index >= ll.size:
|
||||
return -1
|
||||
|
||||
if index == 0: # Remove the first node
|
||||
ll.head = ll.head.next
|
||||
else:
|
||||
pre = findNode(ll, index - 1)
|
||||
if pre is None or pre.next is None:
|
||||
return -1
|
||||
pre.next = pre.next.next
|
||||
|
||||
ll.size -= 1
|
||||
return 0
|
||||
|
||||
def removeAllItems(ll):
|
||||
ll.head = None
|
||||
ll.size = 0
|
||||
|
||||
def moveMaxToFront(ll):
|
||||
if ll.head is None or ll.head.next is None: # Empty list or single node
|
||||
return -1
|
||||
|
||||
pre_max = None
|
||||
max_node = ll.head
|
||||
cur = ll.head
|
||||
|
||||
while cur.next is not None:
|
||||
if cur.next.item > max_node.item:
|
||||
pre_max = cur
|
||||
max_node = cur.next
|
||||
cur = cur.next
|
||||
|
||||
if pre_max is None: # Max node is already at the front
|
||||
return 0
|
||||
|
||||
# Move the max node to the front
|
||||
pre_max.next = max_node.next
|
||||
max_node.next = ll.head
|
||||
ll.head = max_node
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
ll = LinkedList()
|
||||
|
||||
while True:
|
||||
print("1: Insert an integer to the linked list:")
|
||||
print("2: Move the largest stored value to the front of the list:")
|
||||
print("0: Quit:")
|
||||
|
||||
choice = int(input("Please input your choice (1/2/0): "))
|
||||
|
||||
if choice == 1:
|
||||
value = int(input("Input an integer that you want to add to the linked list: "))
|
||||
insertNode(ll, ll.size, value)
|
||||
print("The resulting linked list is:")
|
||||
printList(ll)
|
||||
elif choice == 2:
|
||||
moveMaxToFront(ll)
|
||||
print("The resulting linked list after moving the largest stored value to the front of the list is:")
|
||||
printList(ll)
|
||||
elif choice == 0:
|
||||
removeAllItems(ll)
|
||||
break
|
||||
else:
|
||||
print("Choice unknown.")
|
|
@ -0,0 +1,123 @@
|
|||
class ListNode:
|
||||
def __init__(self, item):
|
||||
self.item = item
|
||||
self.next = None
|
||||
|
||||
class LinkedList:
|
||||
def __init__(self):
|
||||
self.size = 0
|
||||
self.head = None
|
||||
|
||||
# Function to remove duplicates from a sorted linked list
|
||||
def remove_duplicates_sorted_ll(ll):
|
||||
current = ll.head
|
||||
if current is None:
|
||||
return
|
||||
|
||||
while current.next is not None:
|
||||
if current.item == current.next.item:
|
||||
temp = current.next.next
|
||||
current.next = temp
|
||||
ll.size -= 1
|
||||
else:
|
||||
current = current.next
|
||||
|
||||
def print_list(ll):
|
||||
current = ll.head
|
||||
if current is None:
|
||||
print("Empty")
|
||||
return
|
||||
|
||||
while current is not None:
|
||||
print(current.item, end=" ")
|
||||
current = current.next
|
||||
print()
|
||||
|
||||
def remove_all_items(ll):
|
||||
current = ll.head
|
||||
while current is not None:
|
||||
temp = current.next
|
||||
current = None
|
||||
current = temp
|
||||
|
||||
ll.head = None
|
||||
ll.size = 0
|
||||
|
||||
def find_node(ll, index):
|
||||
if ll is None or index < 0 or index >= ll.size:
|
||||
return None
|
||||
|
||||
temp = ll.head
|
||||
while index > 0:
|
||||
temp = temp.next
|
||||
if temp is None:
|
||||
return None
|
||||
index -= 1
|
||||
|
||||
return temp
|
||||
|
||||
def insert_node(ll, index, value):
|
||||
if ll is None or index < 0 or index > ll.size:
|
||||
return -1
|
||||
|
||||
new_node = ListNode(value)
|
||||
|
||||
# If inserting at the head
|
||||
if index == 0:
|
||||
new_node.next = ll.head
|
||||
ll.head = new_node
|
||||
ll.size += 1
|
||||
return 0
|
||||
|
||||
# Insert at other positions
|
||||
prev_node = find_node(ll, index - 1)
|
||||
if prev_node is not None:
|
||||
new_node.next = prev_node.next
|
||||
prev_node.next = new_node
|
||||
ll.size += 1
|
||||
return 0
|
||||
|
||||
return -1
|
||||
|
||||
def remove_node(ll, index):
|
||||
if ll is None or index < 0 or index >= ll.size:
|
||||
return -1
|
||||
|
||||
# Remove head node
|
||||
if index == 0:
|
||||
ll.head = ll.head.next
|
||||
ll.size -= 1
|
||||
return 0
|
||||
|
||||
# Remove other nodes
|
||||
prev_node = find_node(ll, index - 1)
|
||||
if prev_node is not None and prev_node.next is not None:
|
||||
prev_node.next = prev_node.next.next
|
||||
ll.size -= 1
|
||||
return 0
|
||||
|
||||
return -1
|
||||
|
||||
if __name__ == "__main__":
|
||||
ll = LinkedList()
|
||||
|
||||
while True:
|
||||
print("1: Insert an integer to the linked list:")
|
||||
print("2: Remove duplicates from a sorted linked list:")
|
||||
print("0: Quit:")
|
||||
choice = int(input("Please input your choice(1/2/0): "))
|
||||
|
||||
if choice == 1:
|
||||
value = int(input("Input an integer that you want to add to the linked list: "))
|
||||
insert_node(ll, ll.size, value)
|
||||
print("The resulting linked list is: ")
|
||||
print_list(ll)
|
||||
elif choice == 2:
|
||||
remove_duplicates_sorted_ll(ll)
|
||||
print("The resulting linked list after removing duplicate values from the sorted linked list is: ")
|
||||
print_list(ll)
|
||||
elif choice == 0:
|
||||
remove_all_items(ll)
|
||||
break
|
||||
else:
|
||||
print("Choice unknown;")
|
Loading…
Reference in New Issue