mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-22 13:47:37 +00:00
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 <mithridatus@mail.ru>
This commit is contained in:
parent
9891d2bc30
commit
11a61d15dc
@ -10,12 +10,14 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Self
|
from typing import Self, TypeVar
|
||||||
|
|
||||||
|
DataType = TypeVar("DataType")
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Node:
|
class Node[DataType]:
|
||||||
data: int
|
data: DataType
|
||||||
previous: Self | None = None
|
previous: Self | None = None
|
||||||
next: Self | None = None
|
next: Self | None = None
|
||||||
|
|
||||||
@ -52,7 +54,7 @@ class LinkedList:
|
|||||||
current = current.next
|
current = current.next
|
||||||
return " ".join(str(node) for node in nodes)
|
return " ".join(str(node) for node in nodes)
|
||||||
|
|
||||||
def __contains__(self, value: int):
|
def __contains__(self, value: DataType):
|
||||||
current = self.head
|
current = self.head
|
||||||
while current:
|
while current:
|
||||||
if current.data == value:
|
if current.data == value:
|
||||||
@ -87,7 +89,7 @@ class LinkedList:
|
|||||||
else:
|
else:
|
||||||
self.insert_after_node(self.tail, node)
|
self.insert_after_node(self.tail, node)
|
||||||
|
|
||||||
def insert(self, value: int) -> None:
|
def insert(self, value: DataType) -> None:
|
||||||
node = Node(value)
|
node = Node(value)
|
||||||
if self.head is None:
|
if self.head is None:
|
||||||
self.set_head(node)
|
self.set_head(node)
|
||||||
@ -116,7 +118,7 @@ class LinkedList:
|
|||||||
|
|
||||||
node.next = node_to_insert
|
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
|
current_position = 1
|
||||||
new_node = Node(value)
|
new_node = Node(value)
|
||||||
node = self.head
|
node = self.head
|
||||||
@ -128,7 +130,7 @@ class LinkedList:
|
|||||||
node = node.next
|
node = node.next
|
||||||
self.set_tail(new_node)
|
self.set_tail(new_node)
|
||||||
|
|
||||||
def get_node(self, item: int) -> Node:
|
def get_node(self, item: DataType) -> Node:
|
||||||
node = self.head
|
node = self.head
|
||||||
while node:
|
while node:
|
||||||
if node.data == item:
|
if node.data == item:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user