Post

백준 18258번 python 풀이 - 큐2

문제 링크

18258번

해결책

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys


class Node:
    def __init__(self, val: int):
        self.val = val
        self.ptr = None


class Queue:
    def __init__(self, limit: int = 1000):
        self.start = None
        self.end = None
        self.len = 0

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

    def Size(self):
        return self.len

    def Front(self):
        if(self.len <= 0):
            return -1
        return self.start.val

    def Back(self):
        if(self.len <= 0):
            return -1
        return self.end.val

    def queue(self, val):
        node = Node(val)
        if self.len == 0:
            self.start = node
        else:
            self.end.ptr = node
        self.len += 1
        self.end = node

    def dequeue(self):
        if self.len <= 0:
            return -1
        val = self.start.val
        self.start = self.start.ptr
        self.len -= 1
        if self.len <= 0:
            self.start = None
            self.end = None
        return val


N = int(sys.stdin.readline().rstrip())
que = Queue()
ans = []

for i in range(N):

    code = sys.stdin.readline().rstrip()
    if " " in code:
        order, num = code.split()
        num = int(num)
    else:
        order = code

    if order == "push":
        que.queue(num)
    elif order == "pop":
        ans.append(que.dequeue())
    elif order == "front":
        ans.append(que.Front())
    elif order == "back":
        ans.append(que.Back())
    elif order == "size":
        ans.append(que.Size())
    elif order == "empty":
        ans.append(que.empty())

for i in ans:
    print(i)

주석으로 달 설명

이전에 만든 연결 리스트로 구현한 큐를 기반으로 제작했다. 딱히 그 이상은 없다…

This post is licensed under CC BY 4.0 by the author.