bj10828 스택

coh·2022년 6월 6일
0

백준

목록 보기
16/27

stack을 구현하는 문제였음. 그래서 stack class를 만들어서 구현하였음. 음.. 사실 이거 그냥 list 써도 되는데 자료구조 공부하려고 stack을 구현해서 풀었음!! ㅎㅎ

import sys
sys.stdin = open('10828.txt', 'r')


class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None


class stack:
    def __init__(self):
        self.head = None
        self.size = 0

    def push(self, data):
        node = Node(data)
        if self.head:
            node.next = self.head
        self.head = node
        self.size += 1

    def pop(self):
        if self.head is None:
            return -1
        data = self.head.data
        self.head = self.head.next
        self.size -= 1
        return data

    def check_size(self):
        return self.size

    def empty(self):
        if self.size == 0:
            return 1
        else:
            return 0

    def top(self):
        if self.size == 0:
            return -1
        return self.head.data


def judge(oper):
    if oper[0] == 'push':
        Stack.push(oper[1])
    elif oper[0] == 'top':
        print(Stack.top())
    elif oper[0] == 'size':
        print(Stack.check_size())
    elif oper[0] == 'pop':
        print(Stack.pop())
    else:
        print(Stack.empty())


n = int(input())
Stack = stack()
for _ in range(n):
    a = list(map(str, sys.stdin.readline().split()))
    judge(a[:])
profile
Written by coh

0개의 댓글