queues n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Queues PowerPoint Presentation
Download Presentation
Queues

Loading in 2 Seconds...

play fullscreen
1 / 38

Queues - PowerPoint PPT Presentation


  • 148 Views
  • Uploaded on

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. Bus Stop. Bus Stop Queue. front. rear. rear. rear. rear. rear. Bus Stop. Bus Stop Queue. front. rear. rear. rear.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Queues' - adonai


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
queues
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.
bus stop queue

Bus Stop

Bus Stop Queue

front

rear

rear

rear

rear

rear

bus stop queue1

Bus Stop

Bus Stop Queue

front

rear

rear

rear

revisit of stack applications
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.
lee s wire router

start pin

end pin

Lee’s Wire Router

Label all reachable squares 1 unit from start.

lee s wire router1

start pin

end pin

Lee’s Wire Router

1

1

Label all reachable unlabeled squares 2 units from start.

lee s wire router2

start pin

end pin

Lee’s Wire Router

2

2

1

1

2

2

2

Label all reachable unlabeled squares 3 units from start.

lee s wire router3

start pin

end pin

Lee’s Wire Router

3

3

2

2

1

1

2

2

2

3

3

Label all reachable unlabeled squares 4 units from start.

lee s wire router4

start pin

end pin

Lee’s Wire Router

4

3

3

2

2

1

1

2

2

2

3

4

3

4

4

4

Label all reachable unlabeled squares 5 units from start.

lee s wire router5

start pin

end pin

Lee’s Wire Router

5

4

5

3

3

2

2

1

1

2

2

2

3

4

3

4

5

4

4

5

5

5

Label all reachable unlabeled squares 6 units from start.

lee s wire router6

start pin

end pin

Lee’s Wire Router

6

5

6

4

5

3

3

2

2

1

1

2

2

2

6

3

4

3

4

5

6

4

4

5

6

5

6

5

6

6

End pin reached. Traceback.

lee s wire router7

start pin

end pin

1

2

3

4

Lee’s Wire Router

6

5

6

4

5

3

3

2

2

1

1

2

2

2

6

3

4

3

4

5

5

6

4

4

5

6

5

6

5

6

6

End pin reached. Traceback.

queue operations
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
queue in an array
Queue in an Array
  • Use a 1D array to represent a queue.
  • Suppose queue elements are stored with the front element in queue[0], the next in queue[1], and so on.
queue in an array1

a

b

c

d

e

0

1

2

3

4

5

6

Queue in an Array
  • DeleteQ() => delete queue[0]
    • O(queue size) time
  • AddQ(x) => if there is capacity, add at right end
    • O(1) time
o 1 addq and deleteq
O(1) AddQ and DeleteQ
  • to perform each opertion in O(1) time (excluding array doubling), we use a circular representation.
circular array

queue[]

[2]

[3]

[1]

[4]

[0]

[5]

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

[2]

[3]

A

B

C

[1]

[4]

[0]

[5]

Circular Array
  • Possible configuration with 3 elements.
circular array2

[2]

[3]

C

[1]

[4]

B

A

[0]

[5]

Circular Array
  • Another possible configuration with 3 elements.
circular array3

[2]

[3]

[2]

[3]

A

B

rear

rear

front

front

C

C

[1]

[4]

[1]

[4]

B

A

[0]

[5]

[0]

[5]

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

[2]

[3]

A

B

rear

front

C

[1]

[4]

[0]

[5]

Add An Element
  • Move rear one clockwise.
add an element1

[2]

[3]

A

B

front

C

[1]

[4]

[0]

[5]

rear

Add An Element
  • Move rear one clockwise.
  • Then put into queue[rear].

D

delete an element

[2]

[3]

A

B

rear

front

C

[1]

[4]

[0]

[5]

Delete An Element
  • Move front one clockwise.
delete an element1

[2]

[3]

front

A

B

rear

C

[1]

[4]

[0]

[5]

Delete An Element
  • Move front one clockwise.
  • Then extract from queue[front].
moving rear clockwise

[2]

[3]

A

B

rear

front

C

[1]

[4]

[0]

[5]

Moving rear Clockwise
  • rear++;
  • if(rear = = capacity) rear = 0;
  • rear = (rear + 1) % capacity;
empty that queue

[2]

[3]

rear

front

C

[1]

[4]

B

A

[0]

[5]

Empty That Queue
empty that queue1

[2]

[3]

rear

C

[1]

[4]

B

[0]

[5]

Empty That Queue

front

empty that queue2

[2]

[3]

rear

C

[1]

[4]

[0]

[5]

Empty That Queue

front

empty that queue3

[2]

[3]

rear

[1]

[4]

[0]

[5]

Empty That Queue
  • 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.

front

a full tank please

[2]

[3]

rear

front

C

[1]

[4]

B

A

[0]

[5]

A Full Tank Please
a full tank please1

[2]

[3]

rear

front

[1]

[4]

[0]

[5]

A Full Tank Please

D

C

B

A

a full tank please2

[2]

[3]

front

[1]

[4]

[0]

[5]

A Full Tank Please

rear

D

E

C

B

A

a full tank please3

[2]

[3]

front

[1]

[4]

[0]

[5]

A Full Tank Please

D

E

C

F

B

A

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!
slide37
Ouch!!!!!
  • Remedies.
    • Don’t 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
slide38
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.