[CircularQueue][Python]원형큐 구현하기

 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
def isEmpty():
    return front == rear

def isFull():
    return (rear+1) % len(cQ) == front

def enQueue(item):
    global rear
    if isFull():
        print("Queue is Full")
    else:
        rear = (rear+1) % len(cQ)
        cQ[rear] = item

def deQueue():
    global front
    if isEmpty():
        print("Queue is Empty")
    else:
        front = (front+1) % len(cQ)
        return cQ[front]


cQ_size = 4
cQ = [0] * cQ_size
front = rear = 0

enQueue('A') # front 0 rear 1 [0, 'A', 0, 0]
enQueue('B') # front 0 rear 2 [0, 'A', 'B', 0]
print(deQueue()) # 'A' # front 1 rear 2 [0, 'A', 'B', 0]
enQueue('C') # front 1 rear 3 [0, 'A', 'B', 'C']
enQueue('D') # front 1 rear 0 ['D', 'A', 'B', 'C']
enQueue('E') # Queue is Full -> (rear+1) % 4 == front
print(deQueue()) # 'B' # front 2 rear 0 ['D', 'A', 'B', 'C']
enQueue('E') # front 2 rear 1 ['D', 'E', 'B', 'C']

댓글

이 블로그의 인기 게시물

[python]섬의 둘레구하기

백트래킹으로 부분집합구하기(Get Powerset usiing Backtracking)

[Python]DFS를 이용한 부분집합 구현