Python/data_structures/linked_list/swap_nodes.py

149 lines
4.2 KiB
Python
Raw Normal View History

from __future__ import annotations
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Any
@dataclass
class Node:
data: Any
next_node: Node | None = None
@dataclass
class LinkedList:
head: Node | None = None
def __iter__(self) -> Iterator:
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
"""
>>> linked_list = LinkedList()
>>> list(linked_list)
[]
>>> linked_list.push(0)
>>> tuple(linked_list)
(0,)
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
"""
node = self.head
while node:
yield node.data
node = node.next_node
def __len__(self) -> int:
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
"""
>>> linked_list = LinkedList()
>>> len(linked_list)
0
>>> linked_list.push(0)
>>> len(linked_list)
1
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
"""
return sum(1 for _ in 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
def push(self, new_data: Any) -> None:
"""
Add a new node with the given data to the beginning of the Linked List.
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
Args:
new_data (Any): The data to be added to the new node.
Returns:
None
Examples:
>>> linked_list = LinkedList()
>>> linked_list.push(5)
>>> linked_list.push(4)
>>> linked_list.push(3)
>>> linked_list.push(2)
>>> linked_list.push(1)
>>> list(linked_list)
[1, 2, 3, 4, 5]
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
"""
new_node = Node(new_data)
new_node.next_node = self.head
self.head = new_node
def swap_nodes(self, node_data_1: Any, node_data_2: Any) -> None:
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 positions of two nodes in the Linked List based on their data values.
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
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.
Examples:
When both values are present in a linked list.
>>> linked_list = LinkedList()
>>> linked_list.push(5)
>>> linked_list.push(4)
>>> linked_list.push(3)
>>> linked_list.push(2)
>>> linked_list.push(1)
>>> list(linked_list)
[1, 2, 3, 4, 5]
>>> linked_list.swap_nodes(1, 5)
>>> tuple(linked_list)
(5, 2, 3, 4, 1)
When one value is present and the other isn't in the linked list.
>>> second_list = LinkedList()
>>> second_list.push(6)
>>> second_list.push(7)
>>> second_list.push(8)
>>> second_list.push(9)
>>> second_list.swap_nodes(1, 6) is None
True
When both values are absent in the linked list.
>>> second_list = LinkedList()
>>> second_list.push(10)
>>> second_list.push(9)
>>> second_list.push(8)
>>> second_list.push(7)
>>> second_list.swap_nodes(1, 3) is None
True
When linkedlist is empty.
>>> second_list = LinkedList()
>>> second_list.swap_nodes(1, 3) is None
True
Returns:
None
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
"""
if node_data_1 == node_data_2:
return
node_1 = self.head
while node_1 and node_1.data != node_data_1:
node_1 = node_1.next_node
node_2 = self.head
while node_2 and node_2.data != node_data_2:
node_2 = node_2.next_node
if node_1 is None or node_2 is None:
return
# 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__":
"""
Python script that outputs the swap of nodes in a linked list.
"""
from doctest import testmod
testmod()
linked_list = LinkedList()
for i in range(5, 0, -1):
linked_list.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(f"Original Linked List: {list(linked_list)}")
linked_list.swap_nodes(1, 4)
print(f"Modified Linked List: {list(linked_list)}")
print("After swapping the nodes whose data is 1 and 4.")