Queues - PowerPoint PPT Presentation

About This Presentation
Title:

Queues

Description:

Label all reachable squares 1 unit from start. Lee's Wire Router. start pin. end pin ... Label all reachable unlabeled squares 3 units from start. 1. 1. 2. 2. 2 ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 39
Provided by: programmi
Category:
Tags: queues | reachable

less

Transcript and Presenter's Notes

Title: Queues


1
Queues
  • Linear list.
  • One end is called front.
  • Other end is called rear.
  • Additions are done at the rear only.
  • Removals are made from the front only.

2
Bus Stop Queue
front
rear
rear
rear
rear
rear
3
Bus Stop Queue
front
rear
rear
rear
4
Bus Stop Queue
front
rear
rear
5
Bus Stop Queue
front
rear
rear
6
Revisit Of Stack Applications
  • Applications in which the stack cannot be
    replaced with a queue.
  • Parentheses matching.
  • Towers of Hanoi.
  • Switchbox routing.
  • Method invocation and return.
  • Try-catch-throw implementation.
  • Application in which the stack may be replaced
    with a queue.
  • Rat in a maze.
  • Results in finding shortest path to exit.

7
Wire Routing
8
Lees Wire Router
Label all reachable squares 1 unit from start.
9
Lees Wire Router
1
1
Label all reachable unlabeled squares 2 units
from start.
10
Lees Wire Router
2
2
1
1
2
2
2
Label all reachable unlabeled squares 3 units
from start.
11
Lees Wire Router
3
3
2
2
1
1
2
2
2
3
3
Label all reachable unlabeled squares 4 units
from start.
12
Lees Wire Router
4
3
3
2
2
1
1
2
2
2
3
3
4
4
4
4
Label all reachable unlabeled squares 5 units
from start.
13
Lees Wire Router
5
4
5
3
3
2
2
1
1
2
2
2
3
3
4
4
5
4
4
5
5
5
Label all reachable unlabeled squares 6 units
from start.
14
Lees Wire Router
5
6
6
4
5
3
3
2
2
1
1
2
2
2
6
3
3
4
4
5
6
4
4
5
6
5
5
6
6
6
End pin reached. Traceback.
15
Lees Wire Router
5
6
6
4
5
3
3
2
2
1
1
2
2
2
6
3
3
4
4
5
6
5
4
4
5
6
5
5
6
6
6
End pin reached. Traceback.
16
Queue Operations
  • IsFullQ return true iff queue is full
  • IsEmptyQ return true iff queue is empty
  • AddQ add an element at the rear of the queue
  • DeleteQ delete and return the front element of
    the queue

17
Queue in an Array
  • Use a 1D array to represent a queue.
  • Suppose queue elements are stored with the front
    element in queue0, the next in queue1, and so
    on.

18
Queue in an Array
  • DeleteQ() gt delete queue0
  • O(queue size) time
  • AddQ(x) gt if there is capacity, add at right end
  • O(1) time

19
O(1) AddQ and DeleteQ
  • to perform each opertion in O(1) time (excluding
    array doubling), we use a circular representation.

20
Circular Array
  • Use a 1D array queue.
  • Circular view of array.

21
Circular Array
  • Possible configuration with 3 elements.

22
Circular Array
  • Another possible configuration with 3 elements.

23
Circular Array
  • Use integer variables front and rear.
  • front is one position counterclockwise from first
    element
  • rear gives position of last element

24
Add An Element
  • Move rear one clockwise.

25
Add An Element
  • Move rear one clockwise.
  • Then put into queuerear.

D
26
Delete An Element
  • Move front one clockwise.

27
Delete An Element
  • Move front one clockwise.
  • Then extract from queuefront.

28
Moving rear Clockwise
  • rear
  • if (rear capacity) rear 0
  • rear (rear 1) capacity

29
Empty That Queue
30
Empty That Queue
front
31
Empty That Queue
front
32
Empty That Queue
front
  • When a series of removes causes the queue to
    become empty, front rear.
  • When a queue is constructed, it is empty.
  • So initialize front rear 0.

33
A Full Tank Please
34
A Full Tank Please
D
C
A
B
35
A Full Tank Please
rear
D
E
C
A
B
36
A Full Tank Please
D
E
C
F
A
B
rear
  • When a series of adds causes the queue to become
    full, front rear.
  • So we cannot distinguish between a full queue and
    an empty queue!

37
Ouch!!!!!
  • Remedies.
  • Dont let the queue get full.
  • When the addition of an element will cause the
    queue to be full, increase array size.
  • This is what the text does.
  • Define a boolean variable lastOperationIsAddQ.
  • Following each AddQ set this variable to true.
  • Following each DeleteQ set to false.
  • Queue is empty iff (front rear)
    !lastOperationIsAddQ
  • Queue is full iff (front rear)
    lastOperationIsAddQ

38
Ouch!!!!!
  • Remedies (continued).
  • Define an integer variable size.
  • Following each AddQ do size.
  • Following each DeleteQ do size--.
  • Queue is empty iff (size 0)
  • Queue is full iff (size arrayLength)
  • Performance is slightly better when first
    strategy is used.
Write a Comment
User Comments (0)
About PowerShow.com