[BOJ] #10828번 스택

2022. 4. 25. 22:180️⃣ Algorithm&자료구조/BOJ

 

 

 

import sys
N = int(sys.stdin.readline())

class stacking:
    def __init__(self):
        self.top = []

    def push(self, a):
        return self.top.append(a)
    
    def pop(self):
        if not self.top: 
            return print(-1)
        else:
            pop_object = self.top.pop()
            return print(pop_object)
    
    def size(self):
        return print(len(self.top))
    
    def empty(self):
        if len(self.top) > 0:
            return print(0)
        else:
            return print(1)

    def ttop(self):
        if not self.top:
            return print(-1)
        else:
            return print(self.top[-1])

stack = stacking()

def solution(s, stack):
    if s[0:2] == 'pu':
        a, b = s.split()
        num = int(b)
        stack.push(num)

    elif s[0:2] == 'po':
        stack.pop()

    elif s[0:2] == 'to':
        stack.ttop()

    elif s[0:2] == 'si':
        stack.size()
    else:
        stack.empty()
        

for i in range(N):
    s = sys.stdin.readline().strip()
    solution(s, stack)

'0️⃣ Algorithm&자료구조 > BOJ' 카테고리의 다른 글

[BOJ] #18258번 큐 2  (0) 2022.04.25
[BOJ] #10773 제로  (0) 2022.04.25
[BOJ] #15652번 - N과 M (4)  (0) 2022.04.16
[BOJ] # 15651번 - N과 M (3)  (0) 2022.04.16
[BOJ] # 15650 - N과 M (2)  (0) 2022.04.15