Professional Documents
Culture Documents
CCH1D4 Struktur Data: Queue
CCH1D4 Struktur Data: Queue
STRUKTUR DATA
Queue
Queue
• Linked list implementation to a real case
2 3/17/2020
Implementation
• client-server systems
3 3/17/2020
Queue – in summary
• Single Linked list or double linked list with only
– Insert Last add, enqueue
– Delete first del, dequeue
4 3/17/2020
ADT Queue Element
Type infotype : integer
Type address : pointer to ElmQueue INFO NEXT
• Example :
idxMax = 5
Type Queue :
1 2 3 4 5
< array [1..idxMax] of integer
Head, Tail : integer >
Q : Queue Head 0 Tail 0
Head(Q) 0; Tail(Q) 0
7 3/17/2020
Alternative I
Add(Q, 5) 1 2 3 4 5
5
2
7
4 2
7
4 7
Add(Q, 2)
Add(Q, 7)
Del(Q)
Head 0
1 Tail 0
1
3
2
Del(Q)
Add(Q, 4)
isEmpty false
true
Del(Q)
Del(Q)
8 3/17/2020
Alternative I
• Add and Remove Element
– Tail move back and forth
9 3/17/2020
Alternative II
Add(Q, 5) 1 2 3 4 5
Add(Q, 2) 5
4 2
9 7
4 4 9
Add(Q, 7)
Del(Q)
Del(Q) Head 0
1
2
3
4 Tail 0
1
2
5
4
3
Add(Q, 4)
Del(Q)
Add(Q, 9)
isEmpty true
false
Add(Q, 4)
Del(Q)
Del(Q)
Del(Q)
10 3/17/2020
Alternative II
• Add and Remove Element
– Both head and tail move back and forth
12 3/17/2020
Alternative III
Add(Q, 5) Add(Q, 8) 5 1
Add(Q, 2) Del(Q) 9 5
4
Add(Q, 7) Del(Q)
Del(Q) Del(Q)
4 2
8
Del(Q) Del(Q) 4 2
Add(Q, 4) 7
Del(Q) 3
Add(Q, 9)
Head 0
1
2
3
4
5 Tail 0
1
2
5
4
3
Add(Q, 4)
isEmpty true
false
13 3/17/2020
Alternative III
Add(Q, 5) Add(Q, 8) 1 2 3 4 5
Add(Q, 2) Del(Q) 5
4 2
8 7 4 9
Add(Q, 7) Del(Q)
Del(Q) Del(Q)
Head 0
1
2
3
4
5 Tail 0
1
2
5
4
3
Del(Q) Del(Q)
Add(Q, 4)
Del(Q)
Add(Q, 9)
Add(Q, 4)
isEmpty true
false
14 3/17/2020
Alternative III
• Add and Remove Element
– Both head and tail always move forward
– When max queue is reached, try to circle around
15 3/17/2020
THANK YOU
3/17/2020
16