Skip to content

Commit 11a61d1

Browse files
Generic type hint in DDL (#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 <[email protected]>
1 parent 9891d2b commit 11a61d1

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Diff for: data_structures/linked_list/doubly_linked_list_two.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
"""
1111

1212
from dataclasses import dataclass
13-
from typing import Self
13+
from typing import Self, TypeVar
14+
15+
DataType = TypeVar("DataType")
1416

1517

1618
@dataclass
17-
class Node:
18-
data: int
19+
class Node[DataType]:
20+
data: DataType
1921
previous: Self | None = None
2022
next: Self | None = None
2123

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

55-
def __contains__(self, value: int):
57+
def __contains__(self, value: DataType):
5658
current = self.head
5759
while current:
5860
if current.data == value:
@@ -87,7 +89,7 @@ def set_tail(self, node: Node) -> None:
8789
else:
8890
self.insert_after_node(self.tail, node)
8991

90-
def insert(self, value: int) -> None:
92+
def insert(self, value: DataType) -> None:
9193
node = Node(value)
9294
if self.head is None:
9395
self.set_head(node)
@@ -116,7 +118,7 @@ def insert_after_node(self, node: Node, node_to_insert: Node) -> None:
116118

117119
node.next = node_to_insert
118120

119-
def insert_at_position(self, position: int, value: int) -> None:
121+
def insert_at_position(self, position: int, value: DataType) -> None:
120122
current_position = 1
121123
new_node = Node(value)
122124
node = self.head
@@ -128,7 +130,7 @@ def insert_at_position(self, position: int, value: int) -> None:
128130
node = node.next
129131
self.set_tail(new_node)
130132

131-
def get_node(self, item: int) -> Node:
133+
def get_node(self, item: DataType) -> Node:
132134
node = self.head
133135
while node:
134136
if node.data == item:

0 commit comments

Comments
 (0)