-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
35 lines (28 loc) · 782 Bytes
/
stack.py
File metadata and controls
35 lines (28 loc) · 782 Bytes
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
# stack_queue.py 에 저장
class Stack:
def __init__(self):
self.items = [] # 데이터 저장을 위한 리스트 준비
def push(self, val):
self.items.append(val)
def pop(self):
try: # pop할 아이템이 없으면
return self.items.pop()
except IndexError: # indexError 발생
print("Stack is empty")
def top(self):
try:
return self.items[-1]
except IndexError:
print("Stack is empty")
def __len__(self): # len()로 호출하면 stack의 item 수 반환
return len(self.items)
def isEmpty(self):
return self.__len__() == 0
# for test
S = Stack()
S.push(10)
S.push(2)
print(S.top())
print(S.pop())
print(len(S))
print(S.isEmpty())