未验证 提交 94f38dd8 编写于 作者: E Edward Nuno 提交者: GitHub

[mypy] Fix type annotations for linked_stack.py (#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: Ngithub-actions <${GITHUB_ACTOR}@users.noreply.github.com>
上级 868c2fa0
......@@ -198,12 +198,12 @@
* [Evaluate Postfix Notations](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/evaluate_postfix_notations.py)
* [Infix To Postfix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_postfix_conversion.py)
* [Infix To Prefix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_prefix_conversion.py)
* [Linked Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/linked_stack.py)
* [Next Greater Element](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/next_greater_element.py)
* [Postfix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/postfix_evaluation.py)
* [Prefix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/prefix_evaluation.py)
* [Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack.py)
* [Stack Using Dll](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_using_dll.py)
* [Stack With Doubly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_doubly_linked_list.py)
* [Stack With Singly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_singly_linked_list.py)
* [Stock Span Problem](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stock_span_problem.py)
* Trie
* [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py)
......
""" A Stack using a linked list like structure """
from __future__ import annotations
from typing import Any
from collections.abc import Iterator
from typing import Generic, TypeVar
T = TypeVar("T")
class Node:
def __init__(self, data):
class Node(Generic[T]):
def __init__(self, data: T):
self.data = data
self.next = None
self.next: Node[T] | None = None
def __str__(self):
def __str__(self) -> str:
return f"{self.data}"
class LinkedStack:
class LinkedStack(Generic[T]):
"""
Linked List Stack implementing push (to top),
pop (from top) and is_empty
......@@ -44,15 +47,15 @@ class LinkedStack:
"""
def __init__(self) -> None:
self.top: Node | None = None
self.top: Node[T] | None = None
def __iter__(self):
def __iter__(self) -> Iterator[T]:
node = self.top
while node:
yield node.data
node = node.next
def __str__(self):
def __str__(self) -> str:
"""
>>> stack = LinkedStack()
>>> stack.push("c")
......@@ -63,7 +66,7 @@ class LinkedStack:
"""
return "->".join([str(item) for item in self])
def __len__(self):
def __len__(self) -> int:
"""
>>> stack = LinkedStack()
>>> len(stack) == 0
......@@ -87,7 +90,7 @@ class LinkedStack:
"""
return self.top is None
def push(self, item: Any) -> None:
def push(self, item: T) -> None:
"""
>>> stack = LinkedStack()
>>> stack.push("Python")
......@@ -101,7 +104,7 @@ class LinkedStack:
node.next = self.top
self.top = node
def pop(self) -> Any:
def pop(self) -> T:
"""
>>> stack = LinkedStack()
>>> stack.pop()
......@@ -125,7 +128,7 @@ class LinkedStack:
self.top = self.top.next
return pop_node.data
def peek(self) -> Any:
def peek(self) -> T:
"""
>>> stack = LinkedStack()
>>> stack.push("Java")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册