https://www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.

www.acmicpc.net

import sys

 

stack_list = []

 

def main():

    #while loop counting num.

    n = int(input())

    stack_pointer = -1

 

    while n>0:

        #input = sys.stdin.readline()

        command = list(map(str,sys.stdin.readline().split()))

       # print(command)

        if 'push' in command:

            stack_list.append(command[1])

            stack_pointer = stack_pointer + 1

        elif 'pop' in command:

            if stack_pointer is -1:

                print('-1')

            else:

                print(stack_list[stack_pointer])

                del stack_list[stack_pointer]

                stack_pointer = stack_pointer-1

            

        elif 'size' in command:

            print(stack_pointer+1)

        elif 'empty' in command:

            if stack_pointer is -1:

                print(1)

            else:

                print(0)

            

        elif 'top' in command:

            if stack_pointer is -1:

                print(-1)

            else:

                print(stack_list[stack_pointer])

        

        

        

        

        n= n-1

 

main()

블로그 이미지

BigJoon

간단 스크립트! 명쾌한 설명

,