-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack_list.py
More file actions
50 lines (41 loc) · 1.39 KB
/
stack_list.py
File metadata and controls
50 lines (41 loc) · 1.39 KB
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
from typing import Generic, TypeVar, List
T = TypeVar('T')
class Stack(Generic[T]):
"""
A stack implementation that uses a Python list as the underlying data store.
"""
def __init__(self) -> None:
self._items: List[T] = []
def push(self, item: T) -> None:
"""Adds an item to the top of the stack."""
self._items.append(item)
def pop(self) -> T:
"""Removes and returns the item at the top of the stack."""
if self.is_empty():
raise IndexError("pop from an empty stack")
return self._items.pop()
def peek(self) -> T:
"""Returns the item at the top of the stack without removing it."""
if self.is_empty():
raise IndexError("peek on an empty stack")
return self._items[-1]
def is_empty(self) -> bool:
"""Returns True if the stack is empty, False otherwise."""
return not self._items
def size(self) -> int:
"""Returns the number of items in the stack."""
return len(self._items)
def __str__(self) -> str:
return str(self._items)
if __name__ == '__main__':
# Example Usage
s = Stack[int]()
print(f"Is stack empty? {s.is_empty()}")
s.push(10)
s.push(20)
s.push(30)
print(f"Stack: {s}")
print(f"Stack size: {s.size()}")
print(f"Peek: {s.peek()}")
print(f"Popped: {s.pop()}")
print(f"Stack after pop: {s}")