[BOJ] #18258번 큐 2

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

 

 

 

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

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        
class LinkedQueue:
    def __init__(self):
        self.front = None
        self.rear = None
        self.size = 0
    
    def push(self, data):
        new_node = Node(data)
        if self.isEmpty():
            self.front = new_node
            self.rear = new_node
        else:
            self.rear.next = new_node
            self.rear = new_node
        self.size += 1
    
    def pop(self):
        if self.isEmpty():
            return -1
        else:
            dequeue_object = self.front
            self.front = self.front.next
        
        if self.front is None:
            self.rear = None
        self.size -= 1
        return dequeue_object.data
    
    def Size(self):
        return self.size
    
    def empty(self):
        if self.front is None:
            return 1
        else:
            return 0
        
    def isEmpty(self):
        is_Empty = False
        if self.front is None:
            is_Empty = True
        else:
            is_Empty = False
        return is_Empty

    def Front(self):
        if self.isEmpty():
            return -1
        else:
            return self.front.data

    def back(self):
        if self.isEmpty():
            return -1
        else:
            return self.rear.data

queue = LinkedQueue()

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

    elif s[0:2] == 'po':
        sys.stdout.write(str(queue.pop()) + '\n')

    elif s[0:2] == 'fr':
        sys.stdout.write(str(queue.Front()) + '\n')

    elif s[0:2] == 'ba':
        sys.stdout.write(str(queue.back()) + '\n')
        
    elif s[0:2] == 'si':
        sys.stdout.write(str(queue.Size()) + '\n')
    else:
        sys.stdout.write(str(queue.empty()) + '\n')

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

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

[BOJ] # 1037번: 약수  (0) 2022.05.02
[BOJ] #2164번 카드 2  (0) 2022.04.25
[BOJ] #10773 제로  (0) 2022.04.25
[BOJ] #10828번 스택  (0) 2022.04.25
[BOJ] #15652번 - N과 M (4)  (0) 2022.04.16