Queue - PowerPoint PPT Presentation

About This Presentation
Title:

Queue

Description:

Queue . ... – PowerPoint PPT presentation

Number of Views:243
Avg rating:3.0/5.0
Slides: 45
Provided by: WincoolV5
Category:
Tags: circular | queue

less

Transcript and Presenter's Notes

Title: Queue


1
?????????????????????
  • Queue

2
Queue
  • ?????????????????
  • ???????????????????????????????
  • ????????????? First in First out (FIFO)
  • ????????????????????????????????? (rear) ??????
    ?????????????????????? (front) ??????????????
  • ??? implement ?????????????? ?????? Array ????
    Linked List

implement
??????
3
Queue
  • ??????????????
  • ???????????????????????? ???????????????????????
  • ?????????????????????????????????
  • ???????????????????????????????????????????
  • ??????????????????????????
  • ???????

4
Queue
  • ???????????
  • ??? pointer 2 ???????????????? ??? front ??? rear
  • front ?????????????????????? ?????????????????
  • Rear ??????????????????????? ????????????????????

??????
5
Queue
  • ????????????????
  • ?????????????????????????(enqueue) ??????? insert
  • ???????????????????? (dequeue) ??????? delete

6
Queue Implementation
  • Array
  • Linked List

7
Queue 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
8
Queue Array Implementation
  • ???????????????????????(enqueue)
  • ??????????????????? ????????????????????????????
    ?????? ??? rear maxQ ?????????????? (????? maxQ
    ?????????????)
  • ?????????? (full) ?????????????????????????????
    ??????????????????????? (queue overflow)
  • ???????????????????? ???????????????????? (rear)
  • ????????????????????????? ????????????????????????
    ???? rear
  • ??????????????????????????????????????? rear ???
    front ?????????

9
Queue 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
???????????????????????????????????
10
Queue Array Implementation
  • ?????????????????? (enqueue)

Empty queue

front rear 0
Enqueue(A)
A
Enqueue(B)
A B
11
Queue Array Implementation
  • ?????????????????? (enqueue) (???)

Enqueue (C)
A B C
Enqueue(D)
A B C D
Enqueue(E)
Error Overflow
12
Queue Array Implementation
  • ???????????????????? (dequeue)
  • ????????????????????? ????????????????????????????
    ??????????? ???????????????????????? front rear
    0
  • ?????????? ????????????????????????????
    ?????????????????????????? (queue underflow)
  • ???????????????????? ??????????????? (front)
  • ??????????????????????????????????????????????????
    ????? front

13
Queue 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 ?????????????????????????????????????
14
Queue Array Implementation
  • ???????????????????? (dequeue)

A B C D
dequeue
B C D
dequeue
C D
15
Queue Array Implementation
  • ???????????????????? (dequeue) (???)

dequeue
D
dequeue

front rear 0
dequeue
Error Underflow
16
Queue Linked List Implementation
front
A
X
D
struct node char
name struct node
link
17
Queue Linked List Implementation
front
A
X
D
F
X
void enqueue (datatype newdata) ...
rear?link newnode rear newnode
18
Queue Linked List Implementation
front
D
F
X
char dequeue () deldata front?data
front front ? link
19
Queue Linked List Implementation
How to detect that a queue is either full or
empty ?
20
Practices 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)
21
Queue
  • ??????????? (Circular Queue)
  • ?????????????????????? ???????????????????????????
    ?
  • ???????????????????????????????????????

????
D E
??????? ???????????????????????????????????
????????? rear maxQ
22
Problem with Array Implementation (size 7)
maxq
Enqueue K
Error Overflow
G
H
J
23
Circular Queue
  • ????????????????????
  • ?????????????????????????? 2 ?????? front ???
    rear ?????????????????????????????????????????
  • ?????????????????????? ?????????????? rear
    ?????????????????????????????? ???????????????????
    ????????????????????????? ????????????????????????
    ?????????????????

24
Circular Queue
  • ???????????????????? (???)
  • ????????????????????????? ???? rear
    ?????????????????????????????? ???????????????????
    ??? ?????? rear ??????????????????????????????????
    ??????????
  • ????????????????????????????????????????????????
    ??????????????????? ?

25
Circular Queue
  • ???????????????????????

D E
Enqueue(A)
A D E
Enqueue(B)
A B D E
26
Circular Queue
  • ?????????????????????????

A B D E
Dequeue
A B E
Dequeue
A B
27
Circular Queue
28
Circular Queue
front1
P
Q
F
O
Enqueue
G
N
Queue is Full
H
M
I
L
J
K
29
Circular Queue
P
Q
F
O
Dequeue
G
N
H
M
I
L
J
K
2 rooms available
30
Circular Queue
P
Q
R
O
Enqueue
S
N
Queue is Full
H
M
I
L
J
K
31
Array 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
32
Array 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
33
Question
How can we know that a circular queue (array and
linked list) is empty or full ?
34
Practices 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)
35
Priority Queue
  • ???????????????? ????????????????
    ???????????????????? ????????????????????????
    (priority) ????????????????????????????? ????
    ????????????????????????????????
    ?????????????????????????????????????????????????
    ???????????????????? ????????????????????????????
    ?????????????????? 100 ???? ??????????????????????
    ?????????? 2 ???? ????????????????????????????????
    ???????

36
Priority Queue
  • ?????????????????????? ??????????????????????????
    ????????????????? ????????????????????????????????
    ??????????????????????????????????????????????????
    ?????????????????????????
  • typedef struct int priority
  • char data
  • Queue
  • Queue priority_queue15

37
Priority Queue
...
rear 5
front 1
38
Priority Queue
...
??????????? W ????????????????????? 2
39
Priority Queue
...
???????? W ????????????????????? 2
40
Practices 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)
41
Priority Queue
  • Array Implementation
  • Linked List Implementation

42
Priority Queue
  • Which kind of implementation either array or
    linked list that we should choose for Priority
    Queue Implementation?

43
Practices 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
???????????????????????????? ???????
???????????? ??????????????????????????????
????? ?????????????
???????????? ??????????????????????????????
Write a Comment
User Comments (0)
About PowerShow.com