Chapter 5: Queues: (Data Structures and Algorithms)
Chapter 5: Queues: (Data Structures and Algorithms)
Chapter 5: Queues: (Data Structures and Algorithms)
Chapter 5: Queues
(Data Structures and Algorithms)
Outline
2 Implement Queue
3 Application of Queue
4 Exercises
11/06/2021 5
ISLab- Intelligent Systems Laboratory 5
ADT Queue Operations
2 Implement Queue
3 Application of Queue
4 Exercises
? Empty
(back+1)%MAX_QUEUE == front
? Full
(back+1)%MAX_QUEUE == front
2 Implement Queue
3 Application of Queue
4 Exercises
aQueue.createQueue()
while (not end of line) {
Read a new character ch
aQueue.enqueue(ch)
}
• History: N men surrounded by enemies. Preferred suicide rather than captured as slaves.
Every men killed the next living men until 1 man is left. That guy (Josephus) then
surrendered (did not tell this initially ‘cos others’d turn on him)
ISLab- Intelligent Systems Laboratory 32
Application: Josephus Problem
• N men, circular arrangement. Kill every second. Where
to sit to survive?
• List winners/survivors for each N to see a pattern
N W(N)
1 1
2 1
3 3
4 1
5 3
6 5
7 7
8 1
9 3
10 5
11 7
12 9
13 11
14 13
15 15
16 1
17 3
int W(int N) {
int a = 0;
while (N > 1) {
N /= 2;
a++;
}
return 2*(N – pow(2, a)) + 1;
}
ISLab- Intelligent Systems Laboratory 38
Application: Josephus Problem