Title: Queue
1?????????????????????
2Queue
- ?????????????????
- ???????????????????????????????
- ????????????? First in First out (FIFO)
- ????????????????????????????????? (rear) ??????
?????????????????????? (front) ?????????????? - ??? implement ?????????????? ?????? Array ????
Linked List
implement
??????
3Queue
- ??????????????
- ???????????????????????? ???????????????????????
- ?????????????????????????????????
- ???????????????????????????????????????????
- ??????????????????????????
- ???????
4Queue
- ???????????
- ??? pointer 2 ???????????????? ??? front ??? rear
- front ?????????????????????? ?????????????????
- Rear ??????????????????????? ????????????????????
??????
5Queue
- ????????????????
- ?????????????????????????(enqueue) ??????? insert
- ???????????????????? (dequeue) ??????? delete
6Queue Implementation
7Queue Array Implementation
- ????????????????????? Array, size 5
front rear 0
A
front rear 1
A B C
front 1
rear 3
B C
front 2
rear 3
8Queue Array Implementation
- ???????????????????????(enqueue)
- ??????????????????? ????????????????????????????
?????? ??? rear maxQ ?????????????? (????? maxQ
?????????????) - ?????????? (full) ?????????????????????????????
??????????????????????? (queue overflow) - ???????????????????? ???????????????????? (rear)
- ????????????????????????? ????????????????????????
???? rear - ??????????????????????????????????????? rear ???
front ?????????
9Queue Array Implementation
- INSERT-Q Algorithm
- front rear 0
- if( rear maxQ)
- write Overflow
- else
- if(front0)
- front ? 1
- rear ? 1
- Qrear ? Item
- else
- rear ? rear 1
- Qrear ? Item
-
????????????????????????
????????????????????????????? ??? front 0
??????????????????? ??????????????????????????
???????? Q ?????????? maxQ ????????????? Item
???????????????????????????????????
10Queue Array Implementation
- ?????????????????? (enqueue)
Empty queue
front rear 0
Enqueue(A)
A
Enqueue(B)
A B
11Queue Array Implementation
- ?????????????????? (enqueue) (???)
Enqueue (C)
A B C
Enqueue(D)
A B C D
Enqueue(E)
Error Overflow
12Queue Array Implementation
- ???????????????????? (dequeue)
- ????????????????????? ????????????????????????????
??????????? ???????????????????????? front rear
0 - ?????????? ????????????????????????????
?????????????????????????? (queue underflow) - ???????????????????? ??????????????? (front)
- ??????????????????????????????????????????????????
????? front
13Queue Array Implementation
- DELETE-Q algorithm
- if( rear 0)
- write Underflow
- else
- if(frontrear)
- front ? 0
- rear ? 0
- else
- front ? front 1
-
?????????????????????????????
???????????????????????????????????????????
??????? front rear ?????????????????????????????
???? ?????????????????????????????????????????????
? ? ????? ??????????????????? front ??? rear
????????? 0 ?????????????????????????????????????
14Queue Array Implementation
- ???????????????????? (dequeue)
A B C D
dequeue
B C D
dequeue
C D
15Queue Array Implementation
- ???????????????????? (dequeue) (???)
dequeue
D
dequeue
front rear 0
dequeue
Error Underflow
16Queue Linked List Implementation
front
A
X
D
struct node char
name struct node
link
17Queue Linked List Implementation
front
A
X
D
F
X
void enqueue (datatype newdata) ...
rear?link newnode rear newnode
18Queue Linked List Implementation
front
D
F
X
char dequeue () deldata front?data
front front ? link
19Queue Linked List Implementation
How to detect that a queue is either full or
empty ?
20Practices 1
Imagine we have a queue of integer, Q, size 6.
Draw picture of both structures after the
following operations 1. enqueue(Q,5)
7. enqueue(Q,15) 2. enqueue(Q,1)
8. enqueue(Q,6) 3.
enqueue(Q,7) 9.
dequeue(Q) 4. enqueue(Q,3)
10. dequeue(Q) 5. dequeue(Q)
11. enqueue(Q,10) 6. dequeue(Q)
12. enqueue(Q,9)
21Queue
- ??????????? (Circular Queue)
- ?????????????????????? ???????????????????????????
? - ???????????????????????????????????????
????
D E
??????? ???????????????????????????????????
????????? rear maxQ
22Problem with Array Implementation (size 7)
maxq
Enqueue K
Error Overflow
G
H
J
23Circular Queue
- ????????????????????
- ?????????????????????????? 2 ?????? front ???
rear ????????????????????????????????????????? - ?????????????????????? ?????????????? rear
?????????????????????????????? ???????????????????
????????????????????????? ????????????????????????
?????????????????
24Circular Queue
- ???????????????????? (???)
- ????????????????????????? ???? rear
?????????????????????????????? ???????????????????
??? ?????? rear ??????????????????????????????????
?????????? - ????????????????????????????????????????????????
??????????????????? ?
25Circular Queue
D E
Enqueue(A)
A D E
Enqueue(B)
A B D E
26Circular Queue
- ?????????????????????????
A B D E
Dequeue
A B E
Dequeue
A B
27Circular Queue
28Circular Queue
front1
P
Q
F
O
Enqueue
G
N
Queue is Full
H
M
I
L
J
K
29Circular Queue
P
Q
F
O
Dequeue
G
N
H
M
I
L
J
K
2 rooms available
30Circular Queue
P
Q
R
O
Enqueue
S
N
Queue is Full
H
M
I
L
J
K
31Array Implementation
F
G
H
I
J
rear1
front4
subprogram enqueue (datatype newdata) 1. rear
? rear1 2. if (rear maxq)
2.1 rear ? 1 3. return
32Array Implementation
F
G
H
I
J
front4
rear1
subprogram dequeue (datatype olddata) 1. front
? front1 2. if (front maxq)
2.1 front ? 1 3. return
33Question
How can we know that a circular queue (array and
linked list) is empty or full ?
34Practices 2
Imagine we have a Circular queue of integer, Q,
size 6. Draw picture of both structures after
the following operations 1. enqueue(Q,5)
7. enqueue(Q,15) 2.
enqueue(Q,1) 8.
enqueue(Q,6) 3. enqueue(Q,7)
9. dequeue(Q) 4. enqueue(Q,3)
10. dequeue(Q) 5. dequeue(Q)
11. enqueue(Q,10) 6. dequeue(Q)
12. enqueue(Q,9)
35Priority Queue
- ???????????????? ????????????????
???????????????????? ????????????????????????
(priority) ????????????????????????????? ????
????????????????????????????????
?????????????????????????????????????????????????
???????????????????? ????????????????????????????
?????????????????? 100 ???? ??????????????????????
?????????? 2 ???? ????????????????????????????????
???????
36Priority Queue
- ?????????????????????? ??????????????????????????
????????????????? ????????????????????????????????
??????????????????????????????????????????????????
????????????????????????? - typedef struct int priority
- char data
- Queue
- Queue priority_queue15
37Priority Queue
...
rear 5
front 1
38Priority Queue
...
??????????? W ????????????????????? 2
39Priority Queue
...
???????? W ????????????????????? 2
40Practices 4
Imagine we have a priority queue of integer, Q
size 5. Draw picture of both structures after
the following operations operation (Q, pri,
data) 1. enqueue(Q,1,7)
7. enqueue(Q,3,1) 2. enqueue(Q,1,4)
8. dequeue(Q,x) 3. enqueue(Q,3,3)
9. dequeue(Q,y) 4.
dequeue(Q,x) 10.
enqueue(Q,2,y) 5. enqueue(Q,2,9)
11. enqueue(Q,3,x) 6. enqueue(Q,1,x)
12. enqueue(Q,2,2)
41Priority Queue
- Array Implementation
- Linked List Implementation
42Priority Queue
- Which kind of implementation either array or
linked list that we should choose for Priority
Queue Implementation?
43Practices 5
Imagine we have a priority circular queue of
integer, Q size 5. Draw picture of both
structures after the following operations 1.
enqueue(Q,1,7) 7.
enqueue(Q,3,1) 2. enqueue(Q,1,4)
8. dequeue(Q,x) 3. enqueue(Q,3,3)
9. dequeue(Q,y) 4.
dequeue(Q,x) 10.
enqueue(Q,2,y) 5. enqueue(Q,2,9)
11. enqueue(Q,3,x) 6. enqueue(Q,1,x)
12. enqueue(Q,2,2)
44???????????????????????????? ???????
???????????? ??????????????????????????????
????? ?????????????
???????????? ??????????????????????????????