2025-08-10 02:16:11 +09:00

65 lines
1.7 KiB
Python

import sys
input = sys.stdin.readline
class Stack:
def __init__(self, maxSize):
self.__size = maxSize
self.__data = [0]*maxSize
self.__top = -1
def stackInfo(self):
if self.__top == -1 :
print("Stack is Empty")
return
print("====현재 스택목록({}/{})====".format(self.__top+1,self.__size))
for i in range(self.__top+1):
print(self.__data[i], end = " ")
print()
def push(self, value):
if self.__top == self.__size - 1 :
print("스택이 꽉찼기 때문에 동작을 수행할 수 없습니다.")
return
self.__top += 1
self.__data[self.__top] = value
print("push 완료")
def pop(self):
if self.__top == -1 :
print("스택이 비어있어서 pop할 값이 없습니다")
return
print(self.__data[self.__top])
self.__top -= 1
def peek(self):
if self.__top == -1 :
print("스택이 비어있어서 최상단 값이 없습니다")
return
print(self.__data[self.__top])
print("스택의 최대사이즈를 입력해줘!")
stack = Stack(int(input()))
endPoint = False
while not endPoint :
print("명령어 목록")
print("push <value> | pop | info | peek | exit")
order = input().split()
match order[0]:
case "push":
stack.push(int(order[1]))
case "pop":
stack.pop()
case "info":
stack.stackInfo()
case "peek":
stack.peek()
case "exit":
print("종료")
endPoint = True
case _:
print("제대로 된 명령어를 입력해주세요.")