class Stack:
def __init__(self, base = []):
self.__items = base
def push(self, val):
self.__items.append(val)
def pop(self):
try:
return self.__items.pop()
except IndexError:
print("Stack is empty")
def top(self):
try:
return self.__items[-1]
except IndexError:
print("Stack is empty")
def __len__(self):
return len(self.__items)
stack = Stack()
stack.push(3)
print(stack.pop())
print(f"length of stack : {len(stack)}")
print(stack.top())
stack = Stack([1, 7])
print(stack.pop())
print(stack.pop())

출처
'CS > 자료구조' 카테고리의 다른 글
Hash (2) (0) | 2022.04.14 |
---|---|
Hash (1) (0) | 2022.04.13 |
Linked List (0) | 2022.04.13 |
Queue (Python) (0) | 2022.03.29 |
Stack : 계산기 (Python) (0) | 2022.03.29 |