-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
33 lines (26 loc) · 752 Bytes
/
stack.py
File metadata and controls
33 lines (26 loc) · 752 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
class Stack:
def __init__(self):
self.items = []
def is_empty(self): # 스택이 비었는가
return len(self.items) == 0
def push(self, item):
self.items.append(item) # 빈칸
def pop(self):
if not self.is_empty():
return self.itemas.pop()
else:
return None
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
return None
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print("스택 크기:", stack.size())
print("스택 최상단요소:", stack.peek())
print("스택에서 꺼낸 요소:", stack.pop())
print("스택 크기:", stack.size())
print("스택이 비었냐?:", stack.is_empty())