Bus Stop Queue Queues Linear list. Bus Stop One end is called - - PDF document

bus stop queue queues
SMART_READER_LITE
LIVE PREVIEW

Bus Stop Queue Queues Linear list. Bus Stop One end is called - - PDF document

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


slide-1
SLIDE 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.

Bus Stop Queue

Bus Stop front rear rear rear rear rear

Bus Stop Queue

Bus Stop front rear rear rear

Bus Stop Queue

Bus Stop front rear rear

Bus Stop Queue

Bus Stop front rear rear

The Interface Queue

public interface Queue { public boolean isEmpty(); public Object getFrontEelement(); public Object getRearEelement(); public void put(Object theObject); public Object remove(); }

slide-2
SLIDE 2

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.

Wire Routing Lee’s Wire Router

start pin end pin

Label all reachable squares 1 unit from start.

Lee’s Wire Router

start pin end pin

Label all reachable unlabeled squares 2 units from start.

1 1

Lee’s Wire Router

start pin end pin

Label all reachable unlabeled squares 3 units from start.

1 1 2 2 2 2 2

Lee’s Wire Router

start pin end pin

Label all reachable unlabeled squares 4 units from start.

1 1 2 2 2 2 2 3 3 3 3

slide-3
SLIDE 3

Lee’s Wire Router

start pin end pin

Label all reachable unlabeled squares 5 units from start.

1 1 2 2 2 2 2 3 3 3 3 4 4 4 4 4

Lee’s Wire Router

start pin end pin

Label all reachable unlabeled squares 6 units from start.

1 1 2 2 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5

Lee’s Wire Router

start pin end pin

End pin reached. Traceback.

1 1 2 2 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 6 6

Lee’s Wire Router

start pin end pin 4

End pin reached. Traceback.

1 1 2 2 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 6 6 3 5 2 1

Derive From ArrayLinearList

when front is left end of list and rear is right end

  • Queue.isEmpty() => super.isEmpty()

– O(1) time

  • getFrontElement() => get(0)

– O(1) time

  • getRearElement() => get(size() - 1)

– O(1) time

  • put(theObject) => add(size(), theObject)

– O(1) time

  • remove() => remove(0)

– O(size) time 1 2 3 4 5 6 a b c d e

Derive From ArrayLinearList

when rear is left end of list and front is right end

  • Queue.isEmpty() => super.isEmpty()

– O(1) time

  • getFrontElement() => get(size() - 1)

– O(1) time

  • getRearElement() => get(0)

– O(1) time

  • put(theObject) => add(0, theObject)

– O(size) time

  • remove() => remove(size() - 1)

– O(1) time 1 2 3 4 5 6 e d c b a

slide-4
SLIDE 4

Derive From ArrayLinearList

to perform each opertion in O(1) time (excluding array doubling), we need a customized array representation.

Derive From ExtendedChain

a b c d e

null

firstNode lastNode front rear

when front is left end of list and rear is right end

  • Queue.isEmpty() => super.isEmpty()

– O(1) time

  • getFrontElement() => get(0)

– O(1) time

Derive From ExtendedChain

a b c d e

null

firstNode lastNode front rear

  • getRearElement() => getLast() … new method

– O(1) time

  • put(theObject) => append(theObject)

– O(1) time

  • remove() => remove(0)

– O(1) time

Derive From ExtendedChain

e d c b a

null

firstNode lastNode rear front

when front is right end of list and rear is left end

  • Queue.isEmpty() => super.isEmpty()

– O(1) time

  • getFrontElement() => getLast()

– O(1) time

Derive From ExtendedChain

a b c d e

null

firstNode lastNode rear front

  • getRearElement() => get(0)

– O(1) time

  • put(theObject) => add(0, theObject)

– O(1) time

  • remove() => remove(size-1)

– O(size) time

Custom Linked Code

  • Develop a linked class for Queue from scratch to

get better preformance than obtainable by deriving from ExtendedChain.

slide-5
SLIDE 5

Custom Array Queue

  • Use a 1D array queue.

queue[]

  • Circular view of array.

[0] [1] [2] [3] [4] [5]

Custom Array Queue

  • Possible configuration with 3 elements.

[0] [1] [2] [3] [4] [5] A B C

Custom Array Queue

  • Another possible configuration with 3

elements.

[0] [1] [2] [3] [4] [5] A B C

Custom Array Queue

  • Use integer variables front and rear.

– front is one position counterclockwise from first element – rear gives position of last element

[0] [1] [2] [3] [4] [5] A B C front rear [0] [1] [2] [3] [4] [5] A B C front rear

Add An Element

[0] [1] [2] [3] [4] [5] A B C front rear

  • Move rear one clockwise.

Add An Element

  • Move rear one clockwise.

[0] [1] [2] [3] [4] [5] A B C front rear

  • Then put into queue[rear].

D

slide-6
SLIDE 6

Remove An Element

[0] [1] [2] [3] [4] [5] A B C front rear

  • Move front one clockwise.

Remove An Element

[0] [1] [2] [3] [4] [5] A B C front rear

  • Move front one clockwise.
  • Then extract from queue[front].

Moving rear Clockwise

[0] [1] [2] [3] [4] [5] A B C front rear

  • rear++;

if (rear = = queue.length) rear = 0;

  • rear = (rear + 1) % queue.length;

Empty That Queue

[0] [1] [2] [3] [4] [5] A B C front rear

Empty That Queue

[0] [1] [2] [3] [4] [5] B C front rear

Empty That Queue

[0] [1] [2] [3] [4] [5] C front rear

slide-7
SLIDE 7

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.

[0] [1] [2] [3] [4] [5] front rear

A Full Tank Please

[0] [1] [2] [3] [4] [5] A B C front rear

A Full Tank Please

[0] [1] [2] [3] [4] [5] A B C front rear D

A Full Tank Please

[0] [1] [2] [3] [4] [5] A B C front rear D E

A Full Tank Please

[0] [1] [2] [3] [4] [5] A B C front rear D E F

  • 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!

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 lastOperationIsPut.

  • Following each put set this variable to true.
  • Following each remove set to false.
  • Queue is empty iff (front == rear) && !lastOperationIsPut
  • Queue is full iff (front == rear) && lastOperationIsPut
slide-8
SLIDE 8

Ouch!!!!!

  • Remedies (continued).

Define an integer variable size.

  • Following each put do size++.
  • Following each remove do size--.
  • Queue is empty iff (size == 0)
  • Queue is full iff (size == queue.length)

Performance is slightly better when first strategy is used.