Stack(스택)
# 스택 프로그램 구현
class Stack:
def __init__(self, max_size):
self.stack = []
self.max_size = max_size
self.top = -1
def isFull(self, item):
return len(self.stack) == self.max_size
def isEmpty(self, item):
return len(self.stack) == 0
def push(self, item):
if self.isFull(self.stack):
print("!!Stack Full!!")
else:
self.top += 1
self.stack.append(item)
def pop(self):
if self.isEmpty(self.stack):
print("Stack Empty")
else:
self.top -= 1
self.stack.pop()
def peek(self):
print(f'Current Top Index >>> {self.top}')
print(f'Current Top Value >>> {self.stack[self.top]}')
def list_show(self):
for i, v in reversed(list(enumerate(self.stack))):
print(f'{i}번 index : {v}')
def main():
s = Stack(10)
s.push(3)
s.push(5)
s.pop()
s.push(8)
s.push(4)
s.push(11)
s.pop()
# 결과 출력
s.peek()
s.list_show()
if __name__ == "__main__":
main()
def main():
s = Stack(10)
s.push(5)
s.pop()
s.push(8)
s.pop() # Stack Empty
s.pop() # 스택이 비어있는 상태에서 pop() 시도
if __name__ == "__main__":
main()
def main():
s = Stack(5) # max_size = 5
s.push(5)
s.push(8)
s.push(4)
s.push(5)
s.push(2) # Stack Full
s.push(11) # 스택 Full 상태에서 push() 시도
if __name__ == "__main__":
main()
def main():
s = Stack(10) # max_size = 10
s.push(5)
s.push(8)
s.push(4)
s.push(5)
s.push(2)
s.push(11)
s.push(8)
s.push(4)
s.push(5)
s.push(2) # Stack Full
# 결과 출력
s.peek()
s.list_show()