-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_adt.py
50 lines (39 loc) · 1.34 KB
/
stack_adt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Stack ADT and an array implementation. Defines a generic abstract
stack with the usual methods.
"""
__author__ = 'Maria Garcia de la Banda, modified by Brendon Taylor and Alexey Ignatiev'
__docformat__ = 'reStructuredText'
from abc import ABC, abstractmethod
from typing import TypeVar, Generic
T = TypeVar('T')
class Stack(ABC, Generic[T]):
""" Abstract Stack class. """
def __init__(self) -> None:
""" Object initializer. """
self.length = 0
@abstractmethod
def push(self, item: T) -> None:
""" Pushes an element to the top of the stack."""
pass
@abstractmethod
def pop(self) -> T:
""" Pops an element from the top of the stack."""
pass
@abstractmethod
def peek(self) -> T:
""" Pops the element at the top of the stack."""
pass
def __len__(self) -> int:
""" Returns the number of elements in the stack."""
return self.length
def is_empty(self) -> bool:
""" Returns True iff the stack is empty. """
return len(self) == 0
@abstractmethod
def is_full(self) -> bool:
""" Returns True iff the stack is full and no element can be pushed. """
pass
def clear(self):
""" Clears all elements from the stack. """
self.length = 0