mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
[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: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
868c2fa0a8
commit
94f38dd88c
|
@ -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)
|
||||
|
|
|
@ -1,19 +1,22 @@
|
|||
""" 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")
|
Loading…
Reference in New Issue
Block a user