ν‹°μŠ€ν† λ¦¬ λ·°

λ°˜μ‘ν˜•

기쑴의 νλŠ” μ„ μž…μ„ μΆœ, 즉 FIFO (First-In First Out)방식이 적용이 λœλ‹€. μš°μ„ μˆœμœ„ νλŠ” 이 방식을 λ”°λ₯΄μ§€ μ•Šκ³  μ›μ†Œλ“€μ˜ μš°μ„ μˆœμœ„μ— 따라 κΊΌλ‚΄λŠ” μžλ£Œκ΅¬μ‘°μ΄λ‹€. λŒ€ν‘œμ μΈ μ‘μš© μ˜ˆλ‘œλŠ” μš΄μ˜μ²΄μ œμ—μ„œ CPU μŠ€μΌ€μ€„λŸ¬λ₯Ό κ΅¬ν˜„ν•  λ•Œ ν˜„μž¬ μ‹€ν–‰ν•  수 μžˆλŠ” μž‘μ—…λ“€ 쀑 κ°€μž₯ μš°μ„ μˆœμœ„κ°€ 높은 것을 골라 μ‹€ν–‰ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜μ„ λ“€ 수 μžˆλ‹€.

 

 

 

μš°μ„ μˆœμœ„ 큐의 κ΅¬ν˜„

μš°μ„ μˆœμœ„ 큐λ₯Ό κ΅¬ν˜„ν•  λ•Œ μ„œλ‘œ λ‹€λ₯Έ 두가지 방식이 κ°€λŠ₯ν•˜λ‹€.

  • 큐에 μ›μ†Œλ₯Ό 넣을 λ•Œ (enqueueν•  λ•Œ) μš°μ„ μˆœμœ„ μˆœμ„œλŒ€λ‘œ μ •λ ¬ν•΄ λ‘λŠ” 방법
  • νμ—μ„œ μ›μ†Œλ₯Ό κΊΌλ‚Ό λ•Œ (dequeueν•  λ•Œ) μš°μ„ μˆœμœ„κ°€ κ°€μž₯ 높은 것을 μ„ νƒν•˜λŠ” 방법

두가지 방법 쀑 1번이 더 μœ λ¦¬ν•˜λ‹€. λ§Œμ•½ 큐에 데이터 μ›μ†Œλ“€μ΄ μš°μ„ μˆœμœ„κ°€ 상관없이 λŠ˜μ–΄μ„œ μžˆλ‹€κ³  ν•˜λ©΄ μš°μ„ μˆœμœ„κ°€ κ°€μž₯ 높은 것을 λ””νν•˜λ €λ©΄ λͺ¨λ“  μ›μ†Œλ“€μ„ λ‹€ μ‚΄νŽ΄λ³΄μ•„μ•Όν•œλ‹€. κ·ΈλŸ¬λ―€λ‘œ 큐의 길이에 λΉ„λ‘€ν•˜λŠ” μ •λ„μ˜ μ‹œκ°„μ΄ μ†Œμš”κ°€ λœλ‹€. μš°μ„ μˆœμœ„ μˆœμ„œλŒ€λ‘œ 정렬을 해두면 큐의 길이에 λΉ„λ‘€ν•΄ μ‹œκ°„μ΄ μ†Œμš”λ˜λŠ”κ²ƒμ€ λ§žμ§€λ§Œ λͺ¨λ“  μ›μ†Œλ₯Ό μ‚΄νŽ΄λ³Ό ν•„μš”κ°€ μ—†μœΌλ―€λ‘œ μ›μ†Œλ₯Ό 넣을 λ•Œ μš°μ„ μˆœμœ„κ°€ 높은 μˆœμ„œλŒ€λ‘œ μ •λ ¬ν•΄ λ‘λŠ” 방법이 더 μœ λ¦¬ν•˜λ‹€.

 

μš°μ„ μˆœμœ„λ₯Ό κ΅¬ν˜„ν•  λ•Œ μ„œλ‘œ λ‹€λ₯Έ 두가지 재료λ₯Ό μ΄μš©ν•  수 μžˆλ‹€.

  • μ„ ν˜•λ°°μ—΄μ„ 이용
  • μ—°κ²° 리슀트λ₯Ό 이용

μš°μ„  μ‹œκ°„μ μœΌλ‘œ 봀을 λ•Œ μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ΄μš©ν•˜λŠ”κ²ƒμ΄ μœ λ¦¬ν•˜λ‹€. 데이터λ₯Ό μ‚½μž…ν•΄μ•Όν•˜λŠ” κ²½μš°κ°€ λΉˆλ²ˆν•˜κ²Œ μΌμ–΄λ‚˜κΈ° λ•Œλ¬Έμ— μ‚½μž…ν–ˆμ„ λ•Œ ν•˜λ‚˜ν•˜λ‚˜μ”© λ°€μ–΄μ€˜μ•Όν•˜λŠ” μ„ ν˜•λ°°μ—΄ λŒ€μ‹  μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 훨씬 λΉ λ₯΄λ‹€. ν•˜μ§€λ§Œ λ©”λͺ¨λ¦¬λ₯Ό μ°¨μ§€ν•˜λŠ” 양을 보면 μ„ ν˜•λ°°μ—΄μ΄ 곡간을 덜 μ°¨μ§€ν•œλ‹€. ν•˜μ§€λ§Œ λ©”λͺ¨λ¦¬λ³΄λ‹€ μ‹œκ°„μ΄ λœκ±Έλ¦¬λŠ”κ²Œ νš¨μœ¨μ μ΄λ―€λ‘œ μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ‚¬μš©ν•΄ κ΅¬ν˜„ν•˜λ„λ‘ ν•œλ‹€.

 

 

 

μ½”λ“œ κ΅¬ν˜„

 

class Node:

    def __init__(self, item):
        self.data = item
        self.prev = None
        self.next = None


class DoublyLinkedList:

    def __init__(self):
        self.nodeCount = 0
        self.head = Node(None)
        self.tail = Node(None)
        self.head.prev = None
        self.head.next = self.tail
        self.tail.prev = self.head
        self.tail.next = None


    def __repr__(self):
        if self.nodeCount == 0:
            return 'LinkedList: empty'

        s = ''
        curr = self.head
        while curr.next.next:
            curr = curr.next
            s += repr(curr.data)
            if curr.next.next is not None:
                s += ' -> '
        return s


    def getLength(self):
        return self.nodeCount


    def traverse(self):
        result = []
        curr = self.head
        while curr.next.next:
            curr = curr.next
            result.append(curr.data)
        return result


    def reverse(self):
        result = []
        curr = self.tail
        while curr.prev.prev:
            curr = curr.prev
            result.append(curr.data)
        return result


    def getAt(self, pos):
        if pos < 0 or pos > self.nodeCount:
            return None

        if pos > self.nodeCount // 2:
            i = 0
            curr = self.tail
            while i < self.nodeCount - pos + 1:
                curr = curr.prev
                i += 1
        else:
            i = 0
            curr = self.head
            while i < pos:
                curr = curr.next
                i += 1

        return curr


    def insertAfter(self, prev, newNode):
        next = prev.next
        newNode.prev = prev
        newNode.next = next
        prev.next = newNode
        next.prev = newNode
        self.nodeCount += 1
        return True


    def insertAt(self, pos, newNode):
        if pos < 1 or pos > self.nodeCount + 1:
            return False

        prev = self.getAt(pos - 1)
        return self.insertAfter(prev, newNode)


    def popAfter(self, prev):
        curr = prev.next
        next = curr.next
        prev.next = next
        next.prev = prev
        self.nodeCount -= 1
        return curr.data


    def popAt(self, pos):
        if pos < 1 or pos > self.nodeCount:
            return None

        prev = self.getAt(pos - 1)
        return self.popAfter(prev)


    def concat(self, L):
        self.tail.prev.next = L.head.next
        L.head.next.prev = self.tail.prev
        self.tail = L.tail

        self.nodeCount += L.nodeCount

class PriorityQueue:
    # μ–‘λ°©ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ΄μš©ν•˜μ—¬ 빈 큐λ₯Ό μ΄ˆκΈ°ν™”
    def __init__(self, x):
        self.queue = DoublyLinkedList()
    
    # 크기λ₯Ό λ°˜ν™˜
    def size(self):
        return self.queue.getLength()

    # λΉ„μ–΄μžˆλŠ”κ°€?
    def isEmpty(self):
        return self.size() == 0
        
    # 데이터 μ‚½μž… μ—°μ‚°
    def enqueue(self, x):
        newNode = Node(x)
        
        # 처음 μ‹œμž‘ν•˜λŠ” μœ„μΉ˜ headμ—μ„œ μ‹œμž‘
        curr = self.queue.head
        
        # λκΉŒμ§€ 가지 μ•Šμ„ 쑰건 && μš°μ„ μˆœμœ„λ₯Ό λΉ„κ΅ν•˜λŠ” 쑰건
        while curr.next != self.queue.tail and x < curr.next.data :
            curr = curr.next
        
        # μ–‘λ°©ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ΄μš©ν•΄ μ‚½μž…! 
        self.queue.insertAfter(curr, newNode)
        
        # [주의] μ–‘λ°©ν–₯ μ—°κ²°λ¦¬μŠ€νŠΈμ˜ getAt()λ©”μ„œλ“œλ₯Ό μ΄μš©ν•˜μ§€ μ•ŠλŠ”λ‹€.
        # why? 
    
    # 데이터 μ‚­μ œ μ—°μ‚°
    def dequeue(self):
        return self.queue.popAt(self.queue.getLength())

    # 첫번째 데이터 λ°˜ν™˜
    def peek(self):
        return self.queue.getAt(self.queue.getLength()).data
        
        
 
λ°˜μ‘ν˜•
λŒ“κΈ€