Skip to content

Commit 94f38dd

Browse files
archaengelgithub-actions
and
github-actions
authored
[mypy] Fix type annotations for linked_stack.py (TheAlgorithms#5576)
* Fix type annotations for linked_stack.py * Replace Optional with inline union type * Rename linked_stack to stack_with_singly_linked_list * Rename stack_using_dll to stack_with_doubly_linked_list * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 868c2fa commit 94f38dd

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

DIRECTORY.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@
198198
* [Evaluate Postfix Notations](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/evaluate_postfix_notations.py)
199199
* [Infix To Postfix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_postfix_conversion.py)
200200
* [Infix To Prefix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_prefix_conversion.py)
201-
* [Linked Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/linked_stack.py)
202201
* [Next Greater Element](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/next_greater_element.py)
203202
* [Postfix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/postfix_evaluation.py)
204203
* [Prefix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/prefix_evaluation.py)
205204
* [Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack.py)
206-
* [Stack Using Dll](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_using_dll.py)
205+
* [Stack With Doubly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_doubly_linked_list.py)
206+
* [Stack With Singly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_singly_linked_list.py)
207207
* [Stock Span Problem](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stock_span_problem.py)
208208
* Trie
209209
* [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)

data_structures/stacks/linked_stack.py data_structures/stacks/stack_with_singly_linked_list.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
""" A Stack using a linked list like structure """
22
from __future__ import annotations
33

4-
from typing import Any
4+
from collections.abc import Iterator
5+
from typing import Generic, TypeVar
56

7+
T = TypeVar("T")
68

7-
class Node:
8-
def __init__(self, data):
9+
10+
class Node(Generic[T]):
11+
def __init__(self, data: T):
912
self.data = data
10-
self.next = None
13+
self.next: Node[T] | None = None
1114

12-
def __str__(self):
15+
def __str__(self) -> str:
1316
return f"{self.data}"
1417

1518

16-
class LinkedStack:
19+
class LinkedStack(Generic[T]):
1720
"""
1821
Linked List Stack implementing push (to top),
1922
pop (from top) and is_empty
@@ -44,15 +47,15 @@ class LinkedStack:
4447
"""
4548

4649
def __init__(self) -> None:
47-
self.top: Node | None = None
50+
self.top: Node[T] | None = None
4851

49-
def __iter__(self):
52+
def __iter__(self) -> Iterator[T]:
5053
node = self.top
5154
while node:
5255
yield node.data
5356
node = node.next
5457

55-
def __str__(self):
58+
def __str__(self) -> str:
5659
"""
5760
>>> stack = LinkedStack()
5861
>>> stack.push("c")
@@ -63,7 +66,7 @@ def __str__(self):
6366
"""
6467
return "->".join([str(item) for item in self])
6568

66-
def __len__(self):
69+
def __len__(self) -> int:
6770
"""
6871
>>> stack = LinkedStack()
6972
>>> len(stack) == 0
@@ -87,7 +90,7 @@ def is_empty(self) -> bool:
8790
"""
8891
return self.top is None
8992

90-
def push(self, item: Any) -> None:
93+
def push(self, item: T) -> None:
9194
"""
9295
>>> stack = LinkedStack()
9396
>>> stack.push("Python")
@@ -101,7 +104,7 @@ def push(self, item: Any) -> None:
101104
node.next = self.top
102105
self.top = node
103106

104-
def pop(self) -> Any:
107+
def pop(self) -> T:
105108
"""
106109
>>> stack = LinkedStack()
107110
>>> stack.pop()
@@ -125,7 +128,7 @@ def pop(self) -> Any:
125128
self.top = self.top.next
126129
return pop_node.data
127130

128-
def peek(self) -> Any:
131+
def peek(self) -> T:
129132
"""
130133
>>> stack = LinkedStack()
131134
>>> stack.push("Java")

0 commit comments

Comments
 (0)