Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Generic type hint in DDL (TheAlgorithms#12677)
* Generic type hint in DDL

Instead of forcing int

* Update doubly_linked_list_two.py

* Update doubly_linked_list_two.py

---------

Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
  • Loading branch information
isidroas and MaximSmolskiy authored Apr 21, 2025
commit 11a61d15dc3aebc69f153adca8568076a25f7110
16 changes: 9 additions & 7 deletions data_structures/linked_list/doubly_linked_list_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"""

from dataclasses import dataclass
from typing import Self
from typing import Self, TypeVar

DataType = TypeVar("DataType")


@dataclass
class Node:
data: int
class Node[DataType]:
data: DataType
previous: Self | None = None
next: Self | None = None

Expand Down Expand Up @@ -52,7 +54,7 @@ def __str__(self):
current = current.next
return " ".join(str(node) for node in nodes)

def __contains__(self, value: int):
def __contains__(self, value: DataType):
current = self.head
while current:
if current.data == value:
Expand Down Expand Up @@ -87,7 +89,7 @@ def set_tail(self, node: Node) -> None:
else:
self.insert_after_node(self.tail, node)

def insert(self, value: int) -> None:
def insert(self, value: DataType) -> None:
node = Node(value)
if self.head is None:
self.set_head(node)
Expand Down Expand Up @@ -116,7 +118,7 @@ def insert_after_node(self, node: Node, node_to_insert: Node) -> None:

node.next = node_to_insert

def insert_at_position(self, position: int, value: int) -> None:
def insert_at_position(self, position: int, value: DataType) -> None:
current_position = 1
new_node = Node(value)
node = self.head
Expand All @@ -128,7 +130,7 @@ def insert_at_position(self, position: int, value: int) -> None:
node = node.next
self.set_tail(new_node)

def get_node(self, item: int) -> Node:
def get_node(self, item: DataType) -> Node:
node = self.head
while node:
if node.data == item:
Expand Down