chapter 7 queues. data structure 2 chapter outline objectives follow and explain queue-based...

31
Chapter 7 Queues

Upload: wade-frake

Post on 16-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Chapter 7

Queues

Page 2: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 2

Chapter Outline

Objectives Follow and explain queue-based algorithms using

the front, rear, entering the queue, and exiting the queue

Use a Queue class to implement stack-based algorithm such as scheduling first come, first serve

Recognize situations that require a priority queue

Contents Introduction to Queues Queue Applications Implementations of the Queue ADT Priority Queues

Page 3: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 3

What is a Queue?

Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front).

A queue is a FIFO “first in, first out” structure.

Page 4: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 4

The Queue ADT

Queue’s method Enqueue : Adding an item to the queue

“Entering the queue” Dequeue : Removing an item from the queue

“Deleting from the queue”

Queue underflow If a program attempts to remove an item from a

n empty queue

Page 5: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 5

Contrasting a Stack and a Queue

A

B

C

D

Input : A B C D

stack

A B C D

top front Rear

Queue

Page 6: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 6

Uses for Queues

Copying a word1. Declare a queue of characters2. While(there are more characters of the word to read) { Read a character Insert the character into the queue }3. While(the queue is not empty) { Get the front character from the queue Write the character to the screen }

Simulation programs such as the traffic ate an intersection Input/Output buffering

Page 7: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 7

Array Implementation of a Queue

Add items at one end of the array and Remove items for other end

Access the used portion of the array at both ends, increasing the size of the used portion at one end and decreasing the size of the used portion at the other end

Need a two variables for tracking the array front rear

Page 8: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 8

Array Implementation of a Queue

Queue items will be in the array components data[front], data[front+1], . . . data[rear] To add an item, increment rear and store the new item To get the next item, retrieve data[front]

• rear is incremented but never decremented ?• Reach the end of the array

• variable front would be incremented• free up the array locations with index values less than front

Page 9: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 9

Array Implementation of a Queue

Way to reuse freed locations Maintain all the queue items so that front is

equal to 0 It is very inefficient

Think of the array as being bent into a circle When the rear index reaches the end of the

array, start using the available locations at the front of the array

The successor of the last array index is the first array index

Page 10: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 10

Array Implementation of a Queue

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

A B C ? ?

00 22front rear

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

? ? C D E

22 44front rear

data

data

Page 11: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 11

Array Implementation of a Queue

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

F ? C D E

22 00front rear

data

Page 12: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 12

Array Implementation of a Queue

Circular array

?

CD

E

F

data

22

00

[0]

[1]

[2][3]

[4]

front

rear

Page 13: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 13

Array Implementation of a Queue

data : Queue’s item are held in data front, rear : private instance variable hold the indexes f

or the front and rear manyItems : records the number of items that are in th

e queue nextIndex : private method

Helper method It is not part of the public specification It is just for our own use in implementing a specific kind of que

ue

Page 14: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 14

Array Implementation of a Queue

Helper Methods When a class requires some small computation

that is likely to be used several times Improve the clarity of code Is private, Does not need to be included in the

documentation

Page 15: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 15

Array Implementation of a Queue

Invariant of the Queue ADT The number of items in the queue is stored in the insta

nce variable manyItems For a nonempty queue, the items are stored in a circula

r array beginning at data[front] and continuing through data[rear]

For an empty queue, manyItems is zero and data is a reference to an array, but we are not using any of the array

Page 16: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 16

Array Implementation of a Queue

ensureCapacity method If the array is already big enough, we return with no wor

k Otherwise, allocate a new larger array and copy the ele

ments from original array to the new array Three cases

If manyItems is zero If manyItems is nonzero and front <= rear If manyItems is nonzero and front > rear

Page 17: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 17

Array Implementation of a Queue

ensureCapacity method

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

C D ? A B

vv

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

A B C D ?

[5] [6] [7] [8]

? ? ? ?

rear front

front rear

Page 18: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 18

Array Implementation of a Queue

public void ensureCapacity(int minimumCapacity)

{

Object biggerArray[];

int n1, n2;

if(data.length >= minimumCapacity)

return;

else if(manyItems == 0)

data = new Object[minimumCapacity];

else if(front <= rear)

{

biggerArray = new Object[minimumCapacity];

System.arraycopy(data, front, biggerArray, front,

manyItems);

data = biggerArray;

}

Page 19: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 19

Array Implementation of a Queue

else

{

biggerArray = new Object[minimumCapacity];

n1 = data.length – front;

n2 = rear + 1;

System.arraycopy(data, front, biggerArray, 0 n1);

System.arraycopy(data, 0, biggerArray, n1, n2);

front = 0;

rear = manyItems - 1;

data = biggerArray;

}

}

Page 20: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 20

Linked List Implementation of a Queue

Front Item Second Item Third Item

null

front rear

Page 21: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 21

Linked List Implementation of a Queue Invariant of the Queue ADT

The numbers of items in the queue is stored in the manyNodes

The items in the queue are stored in a linked list with the front of the queue stored at the head node and the rear of the queue stored at the final node

For a nonempty queue, front is the head reference For an empty queue, both front and rear are the null ref

erence

Page 22: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 22

Linked List Implementation of a Queue Insert method

20 30

nullfront

10

rear

Page 23: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 23

Linked List Implementation of a Queue Insert method public void insert(Object item)

{ if(isEmpty()) { front = new Node(item, null); rear = front; } else { rear.addNodeAfter(item); rear = rear.getLink(); } manyNodes++;}

Page 24: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 24

Linked List Implementation of a Queue getFront method

20 30

nullfront

10

rear

20 30

nullfront

rear

Page 25: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 25

Linked List Implementation of a Queue getFront method

Public Object getFront()

{

Object answer;

if(manyNodes == 0)

throw new NoSuchElementException(“Queue

underflow.”);

answer = front.getData();

front = front.getLink();

ManyNodes--;

if(manyNodes == 0)

rear = null;

return answer;

}

Page 26: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 26

Linked List Implementation of a Queue Forgetting Which End is Which

20 30

nullfront

10

rear

null

What’s the problem?What’s the problem?Case 1) enqueueCase 1) enqueueCase 2) dequeueCase 2) dequeue

Page 27: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 27

Priority Queues

Is a data structure that stores items along with a priority for each item.

Items are removed in order of priorities Ginger Snap, priority 0 Natalie Attired, priority 3 Emanual Transmission, priority 2 Gene Pool, priority 3 Kay Sera, priority 2

Page 28: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 28

Priority Queues

Constructor for the ObjectPriorityQueue Public ObjectPriorityQueue(int highest) Initialize an empty priority queue

getFront public Object getFront() Get the highest priority item, removing it from this priori

ty queue Precondition: this queue is not empty Postcondition : return value is highest priority item of th

is queue and the item has been removed

Page 29: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 29

Priority Queues

Insert public void insert(Object item, int priority) Add a new item to this priority queue Precondition : 0<= priority and priority is no more than t

he highest priority Postcondition : the item has been added to this priority

queue

Page 30: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 30

Priority Queues ADT

What is the best way to take advantage of the existing ordinary queue?

To provide the priority queue with an array of ordinary queues

Suppose highest is 2 Implemented using three ordinary queues One to hold the items with priority 0 Another queue for the items of priority 1 Third queue for the items of priority 2

Page 31: Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Data Structure 31

Priority Queues ADT

Example1) Ginger Snap, priority 0 Natalie Attired, priority 3 Emanual Transmission, priority 2 Gene Pool, priority 3 Kay Sera, priority 2

Queues[3] : Natalie Attired, Gene Pool Queues[2] : Emanual Transmission, Kay Sera Queues[1] : empty Queues[0] : Ginger Snap