스택

Updated:

import sys
input = sys.stdin.readline

class process_stack:
    def __init__(self):
        self.item = []
        self.length = 0
    def push(self, num):
        self.item.append(num)
        self.length +=1
    def pop(self):
        if self.length == 0:
            return -1
        else:
            self.length -=1
            return self.item.pop()
    def size(self):
        return self.length
    def empty(self):
        if self.length == 0:
            return 1
        else:
            return 0
    def top(self):
        if self.length == 0:
            return -1
        else:
            return self.item[self.length-1]

n = int(input())

my_stack = process_stack()

for _ in range(n):
    command = input()
    if command[:4] == "push":
        my_stack.push(int(command[5:]))
    elif command[:3] == "pop":
        print(my_stack.pop())
    elif command[:4] == "size":
        print(my_stack.size())
    elif command[:5] == "empty":
        print(my_stack.empty())
    elif command[:3] == "top":
        print(my_stack.top())

Leave a comment