Python/data_structures/linked_list/swap_nodes.py

85 lines
2.3 KiB
Python
Raw Normal View History

from typing import Any
class Node:
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
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
self.next: Node | None = None # Reference to the next node
class LinkedList:
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-04 09:25:26 +00:00
def __init__(self) -> None:
"""
Initialize an empty Linked List.
"""
self.head: Node | None = None # Reference to the head (first node)
def print_list(self):
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-04 09:25:26 +00:00
"""
Print the elements of the Linked List in order.
"""
temp = self.head
while temp is not None:
print(temp.data, end=" ")
temp = temp.next
print()
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
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.
"""
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
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.
"""
if node_data_1 == node_data_2:
return
else:
node_1 = self.head
while node_1 is not None and node_1.data != node_data_1:
node_1 = node_1.next
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
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-04 09:25:26 +00:00
# Swap the data values of the two nodes
node_1.data, node_2.data = node_2.data, node_1.data
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
ll = LinkedList()
for i in range(5, 0, -1):
ll.push(i)
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-04 09:25:26 +00:00
print("Original Linked List:")
ll.print_list()
ll.swap_nodes(1, 4)
Add Comments (#9668) * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : add comment in circular_linked_list.py and swap_nodes.py * docs : improve comments * docs : improved docs and tested on pre-commit * docs : modified comments * Update circular_linked_list.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs : improved * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/swap_nodes.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update requirements.txt Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/circular_linked_list.py Co-authored-by: Christian Clauss <cclauss@me.com> * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update circular_linked_list.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
2023-10-04 09:25:26 +00:00
print("After swapping the nodes whose data is 1 and 4:")
ll.print_list()