What-is-queue.md


๊ฐœ๋…


๋จผ์ € ์ง‘์–ด ๋„ฃ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋‚˜์˜ค๋Š” FIFO(First In First Out)๊ตฌ์กฐ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹

์šฉ์–ด


  • Front : ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ

  • Rear : ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ

    front, rear์˜ ์ดˆ๊นƒ๊ฐ’ : -1

์—ฐ์‚ฐ


  • FIFO - First In First Out

  • Enqueue : ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋™์ž‘

    • add(item) item์„ ๋ฆฌ์ŠคํŠธ์˜ ๋๋ถ€๋ถ„์— ์ถ”๊ฐ€

      • rear = rear + 1 ํ›„ item ์ถ”๊ฐ€

  • Dequeue : ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋™์ž‘

    • remove : ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐ

      • front = front + 1

  • peek : ํ์—์„œ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜

  • isEmpty : ํ๊ฐ€ ๋น„์–ด์žˆ์„ ๋•Œ true๋ฅผ ๋ฐ˜ํ™˜

    • (front == rear) ? True : False

  • isFull : rear = n - 1

์ข…๋ฅ˜


์„ ํ˜•

  • 1์ฐจ์› ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค.

ํ™˜ํ˜•

  • front์™€ rear์˜ ๊ฐ’์ด ๊ณ„์† ์ฆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ธ์  ๊ฐ€๋Š” ๋ฐฐ์—ด์˜ ๋์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๊ณ  ๋ฐฐ์—ด์˜ ์•ž๋ถ€๋ถ„์ด ๋น„์–ด์žˆ๋”๋ผ๋„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

  • ๋”ฐ๋ผ์„œ ์›(ํ™˜)ํ˜• ํ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

  • front ์•ž์˜ ๋‚จ์€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋Š” ์„ ํ˜•ํ์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์›ํ˜•ํ๋Š” ๋ฐฐ์—ด์˜ ์‹œ์ž‘๊ณผ ๋์ด ์ด์–ด์ ธ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด (rear+1)%arraysize์˜ ํ˜•์‹์œผ๋กœ ํฌ์ธํ„ฐ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.

๊ตฌํ˜„


์„ ํ˜•ํ

class LinearQueue:
    def __init__(self):        
        self.front = -1
        self.rear = -1
    
    def create_queue(self, size):
        self.queue = list([0 for i in range(size)]) #python์˜ list๋Š” null๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— 0์ด ๋นˆ ๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž

    def add(self, item):
        self.rear += 1
        self.queue[self.rear] = item
        
    def remove(self):
        self.front += 1
        self.queue[self.front] = 0

    def peek(self):
        return self.queue[self.rear]

    def isEmpty(self):
        if self.front == self.rear:
            return True
        return False

    def isFull(self):
        if self.rear == len(self.queue) - 1:
            return True
        return False

์›ํ˜• ํ

class CircularQueue:
    def __init__(self):
        self.front = -1
        self.rear = -1

    def create_queue(self, size):
        # python์˜ list๋Š” null๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— 0์ด ๋นˆ ๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž
        self.queue = list([0 for i in range(size)])
        print(len(self.queue))

    def add(self, item):
        if self.isFull:
            print('๋ฐฐ์—ด์ด ๊ฝ‰์ฐผ์Šต๋‹ˆ๋‹ค.')
        else:
            self.rear += 1
            if self.rear >= len(self.queue):
                self.rear %= len(self.queue)
            self.queue[self.rear] = item
        print(self.queue)
        print('front: {} rear: {}'.format(self.front, self.rear))

    def remove(self):
        if self.isEmpty:
            print('๋ฐฐ์—ด์ด ๋น„์—ˆ์Šต๋‹ˆ๋‹ค.')
        else:
            self.front += 1
            if self.front >= len(self.front):
                self.front %= len(self.queue)
            self.queue[self.front] = 0
        print(self.queue)
        print('front: {} rear: {}'.format(self.front, self.rear))

    def peek(self):
        return self.queue[self.rear]

    def isEmpty(self):
        if self.front == self.rear:
            return True
        return False

    def isFull(self):
        if self.rear == len(self.queue) - 1:
            return True
        return False

์‹ค์ œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ๋Š” collections ๋ชจ๋“ˆ์˜ deque๋ฅผ ์‚ฌ์šฉํ•˜์ž!

from collections import deque

queue = deque()

# ๋ฆฌ์ŠคํŠธ์˜ append์™€ ๊ฐ™์€ ๋™์ž‘
queue.append()
# ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ์›์†Œ pop
queue.popleft()

print(queue) #๋จผ์ € ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ
print(queue.reverse()) #๋‚˜์ค‘์— ๋“ค์–ด์˜จ ์›์†Œ๋ถ€ํ„ฐ ์ถœ๋ ฅ

Last updated