Docsity

Queues Three - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Queues Three - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Queues

Overview

• Definition

• Standard Java Queue Operations

• Implementation

• Queue at Work

• References

Standard Java Queue Operations

• Queue()

– Constructs an empty Queue object

• boolean isEmpty()

– Determines whether or not the queue is empty

• void enqueue( Object o )

– Adds a new element at the rear of the queue

• Object dequeue()

– Removes and returns the element from the front of the

queue

Implementation

• A queue uses a so-called circular array.

– Briefly, a circular array is created with an index variable

that increments as follows:

• I = (I+1) % MAX_SIZE

• Instance variables of a general class

– private int front

– private int rear

– private size

– private final int MAX_SIZE

– private Object[] q

Implementation (Cont’)

• public Object dequeue()

if( size == 0 )

throw new NoSuchElementException();

Object hold = q[front];

front = (front + 1) % MAX_SIZE;

size--;

return hold;

• Runtime Analysis: O(1)

Queue at Work

  • • Breadth-First Search: (pseudo code)bfs (Graph G)

{ all vertices of G are first painted white

the graph root is painted gray and put in a queue while (! Queue.isEmpty() )

{ Queue.dequeue( a vertex u )

{^ for all white successors v of u

v is painted gray Queue.enqueue(v);

} u is painted black

}^ }

Queue at Work (Cont’)

All vertices are colored white

A B C D E F

Queue at Work (Cont’)

A

B C

D E F

A

AA

B C

D E F

B C AA B C D E F C D E

AA

B C

D E F

D E F

References

• CS146 Textbook

• http://ciips.ee.uwa.edu.au/~morris/Year2/PLD

S210/queues.html

• http://renaud.waldura.com/portfolio/graph-

algorithms/#bfs