2020-01-14 09:02:15 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
2018-12-25 13:09:36 +00:00
|
|
|
class Node:
|
2023-10-04 09:25:26 +00:00
|
|
|
def __init__(self, data: Any) -> None:
|
|
|
|
"""
|
|
|
|
Initialize a new Node with the given data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The data to be stored in the node.
|
|
|
|
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
self.data = data
|
2023-10-04 16:05:00 +00:00
|
|
|
self.next: Node | None = None # Reference to the next node
|
2018-12-25 13:09:36 +00:00
|
|
|
|
|
|
|
|
2020-01-14 09:02:15 +00:00
|
|
|
class LinkedList:
|
2023-10-04 09:25:26 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
"""
|
|
|
|
Initialize an empty Linked List.
|
|
|
|
"""
|
2023-10-04 16:05:00 +00:00
|
|
|
self.head: Node | None = None # Reference to the head (first node)
|
2018-12-25 13:09:36 +00:00
|
|
|
|
|
|
|
def print_list(self):
|
2023-10-04 09:25:26 +00:00
|
|
|
"""
|
|
|
|
Print the elements of the Linked List in order.
|
|
|
|
"""
|
2018-12-25 13:09:36 +00:00
|
|
|
temp = self.head
|
|
|
|
while temp is not None:
|
2020-01-18 12:24:33 +00:00
|
|
|
print(temp.data, end=" ")
|
2018-12-25 13:09:36 +00:00
|
|
|
temp = temp.next
|
2020-01-14 09:02:15 +00:00
|
|
|
print()
|
2018-12-25 13:09:36 +00:00
|
|
|
|
2023-10-04 09:25:26 +00:00
|
|
|
def push(self, new_data: Any) -> None:
|
|
|
|
"""
|
|
|
|
Add a new node with the given data to the beginning of the Linked List.
|
|
|
|
Args:
|
|
|
|
new_data (Any): The data to be added to the new node.
|
|
|
|
"""
|
2018-12-25 13:09:36 +00:00
|
|
|
new_node = Node(new_data)
|
|
|
|
new_node.next = self.head
|
|
|
|
self.head = new_node
|
|
|
|
|
2023-10-04 09:25:26 +00:00
|
|
|
def swap_nodes(self, node_data_1, node_data_2) -> None:
|
|
|
|
"""
|
|
|
|
Swap the positions of two nodes in the Linked List based on their data values.
|
|
|
|
Args:
|
|
|
|
node_data_1: Data value of the first node to be swapped.
|
|
|
|
node_data_2: Data value of the second node to be swapped.
|
|
|
|
|
|
|
|
|
|
|
|
Note:
|
|
|
|
If either of the specified data values isn't found then, no swapping occurs.
|
|
|
|
"""
|
2020-01-14 09:02:15 +00:00
|
|
|
if node_data_1 == node_data_2:
|
2018-12-25 13:09:36 +00:00
|
|
|
return
|
|
|
|
else:
|
2020-01-14 09:02:15 +00:00
|
|
|
node_1 = self.head
|
|
|
|
while node_1 is not None and node_1.data != node_data_1:
|
|
|
|
node_1 = node_1.next
|
2018-12-25 13:09:36 +00:00
|
|
|
|
2020-01-14 09:02:15 +00:00
|
|
|
node_2 = self.head
|
|
|
|
while node_2 is not None and node_2.data != node_data_2:
|
|
|
|
node_2 = node_2.next
|
|
|
|
|
|
|
|
if node_1 is None or node_2 is None:
|
|
|
|
return
|
2018-12-25 13:09:36 +00:00
|
|
|
|
2023-10-04 09:25:26 +00:00
|
|
|
# Swap the data values of the two nodes
|
2020-01-14 09:02:15 +00:00
|
|
|
node_1.data, node_2.data = node_2.data, node_1.data
|
2018-12-25 13:09:36 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2020-01-14 09:02:15 +00:00
|
|
|
ll = LinkedList()
|
|
|
|
for i in range(5, 0, -1):
|
|
|
|
ll.push(i)
|
2018-12-25 13:09:36 +00:00
|
|
|
|
2023-10-04 09:25:26 +00:00
|
|
|
print("Original Linked List:")
|
2020-01-14 09:02:15 +00:00
|
|
|
ll.print_list()
|
2018-12-25 13:09:36 +00:00
|
|
|
|
2020-01-14 09:02:15 +00:00
|
|
|
ll.swap_nodes(1, 4)
|
2023-10-04 09:25:26 +00:00
|
|
|
print("After swapping the nodes whose data is 1 and 4:")
|
|
|
|
|
2020-01-14 09:02:15 +00:00
|
|
|
ll.print_list()
|