1/22/13 CS200 Algorithms and Data Structures Colorado State - - PDF document

1 22 13
SMART_READER_LITE
LIVE PREVIEW

1/22/13 CS200 Algorithms and Data Structures Colorado State - - PDF document

1/22/13 CS200 Algorithms and Data Structures Colorado State University CS200 Algorithms and Data Structures Colorado State University Ou ut tl li in ne e Queue? Implementing Queue Comparison implementations Pa ar rt t


slide-1
SLIDE 1

1/22/13 ¡ 1 ¡

CS200 Algorithms and Data Structures Colorado State University

Pa ar rt t 2 2. . Q Qu ue eu ue es s

CS 200 Algorithms and Data Structures

CS200 Algorithms and Data Structures Colorado State University

Ou ut tl li in ne e

Queue?

Implementing Queue Comparison implementations

8 CS200 Algorithms and Data Structures Colorado State University 9

Photo ¡by ¡David ¡Jump ¡

CS200 Algorithms and Data Structures Colorado State University

"G Gr ri il ll l t th he e B Bu uf ff fs s" " e ev ve en nt t

10 CS200 Algorithms and Data Structures Colorado State University

Qu ue eu ue e

A queue is like a line of people. New item enters a queue at its back. Items leave a queue from its front. First-in, first-out (FIFO) behavior Removing and adding are done from

  • pposite ends of structure

Useful for scheduling (e.g. print queue, job queue)

11

1 ¡ 2 ¡ 3 ¡ 4 ¡ Adding ¡ Removing ¡

CS200 Algorithms and Data Structures Colorado State University

Op pe er ra at ti io

  • n

ns s

Create an empty queue Determine whether a queue is empty Add a new item to the queue Remove item from the queue (that was added the earliest) Remove all items from the queue Retrieve item from queue that was added earliest

12

slide-2
SLIDE 2

1/22/13 ¡ 2 ¡

CS200 Algorithms and Data Structures Colorado State University

Qu ue eu ue e O Op pe er ra at ti io

  • n

ns s

enqueue(in newItem: QueueItemType)

– Add new item at the back of a queue

dequeue();QueueItemType

– Retrieves and removes the item at the front of a queue

peek(): queueItemType {query}

– Retrieve item from the front of the queue. Retrieve the item that was added earliest.

isEmpty():boolean{query} createQueue() dequeueAll()

13 CS200 Algorithms and Data Structures Colorado State University

Ou ut tl li in ne e

Queue?

Implementing Queue

Comparison implementations

14 CS200 Algorithms and Data Structures Colorado State University

Im mp pl le em me en nt ta at ti io

  • n

ns s

  • f

f Q Qu ue eu ue e

Reference-Based Implementation Array-based Implementation List based Implementation java.util.queue interface

15 CS200 Algorithms and Data Structures Colorado State University

. ¡

Re ef fe er re en nc ce e-

  • b

ba as se ed d I Im mp pl le em me en nt ta at ti io

  • n

n( (1 1) )

16

70 ¡ . ¡ 55 ¡ 60 ¡ . ¡

Reference ¡of ¡the ¡Last ¡Node ¡ Reference ¡of ¡the ¡First ¡Node ¡

Needs

– Nodes with the item and a reference to the next item – two external references pointing to the first node and the last node. . ¡

Item ¡ Next ¡

CS200 Algorithms and Data Structures Colorado State University

. ¡

Re ef fe er re en nc ce e-

  • b

ba as se ed d I Im mp pl le em me en nt ta at ti io

  • n

n( (2 2) )

17

70 ¡ . ¡ 55 ¡ 60 ¡ . ¡

Reference ¡of ¡the ¡Last ¡Node ¡

Single external references

– Circular linked list represents a queue – The node at the back of the queue references the node at the front

Reference ¡of ¡the ¡First ¡Node ¡

. ¡

CS200 Algorithms and Data Structures Colorado State University

. ¡

In ns se er rt ti in ng g a an n i it te em m i in nt to

  • a

a n no

  • n

ne em mp pt ty y q qu ue eu ue e

18

70 ¡ . ¡ 55 ¡ 60 ¡ . ¡

Reference ¡of ¡the ¡Last ¡Node ¡

Step 1. newNode.next = lastNode.next; Step 2. lastNode.next = newNode; Step 3. lastNode = newNode; 85 ¡ . ¡

New ¡node ¡

slide-3
SLIDE 3

1/22/13 ¡ 3 ¡

CS200 Algorithms and Data Structures Colorado State University

isEmpty()

public class QueuerReferenceBased implements QueueInterface { private Node lastNode; public QueueReferenceBased(){ lastNode = null; } public boolean isEmpty(){ return lastNode == null; } }

19 CS200 Algorithms and Data Structures Colorado State University

In ns se er rt t n ne ew w i it te em m i in nt to

  • t

th he e q qu ue eu ue e

public void enqueue (Object newItem){ Node newNode = new Node(newItem); if (isEmpty()){ newNode.next = newNode; } else { newNode.next = lastNode.next; lastNode.next = newNode; } lastNode = newNode; }

20

  • A. ¡Empty ¡queue ¡
  • B. ¡More ¡than ¡1 ¡item ¡

CS200 Algorithms and Data Structures Colorado State University

In ns se er rt ti in ng g a a N Ne ew w I It te em m

21

. ¡

Insert a new item into the empty queue

Reference ¡of ¡the ¡Last ¡Node ¡

60 ¡ . ¡

CS200 Algorithms and Data Structures Colorado State University

Reference ¡of ¡New ¡Node ¡

. ¡

In ns se er rt ti in ng g a a N Ne ew w I It te em m

Insert a new item into a queue that has more than 1 items.

22

70 ¡ . ¡ 60 ¡ . ¡ 80 ¡ . ¡

Reference ¡of ¡the ¡Last ¡Node ¡

. ¡

CS200 Algorithms and Data Structures Colorado State University

Re em mo

  • v

vi in ng g a an n i it te em m f fr ro

  • m

m q qu ue eu ue e

public Object dequeue() throws QueueException{ if (!isEmpty()){ Node firstNode = lastNode.next; if (FirstNode == lastnode) { lastNode = null; } else{ lastNode.next = firstNode.next; } return firstNode.item; } else { exception handling.. } }

23

Why? ¡

CS200 Algorithms and Data Structures Colorado State University

Pe ee ek k? ?

public Object peek() throws QueueException { if (!isEmpty()){ Node firstNode = lastNode.next; return firstNode.item; }else { throw new QueueException(your_message); } }

24

slide-4
SLIDE 4

1/22/13 ¡ 4 ¡

CS200 Algorithms and Data Structures Colorado State University

Im mp pl le em me en nt ti in ng g q qu ue eu ue e w wi it th h A Ar rr ra ay y

We need,

– An array of objects to store items – variables to point to the “front” and “back” index of the array

25

0 ¡ 1 ¡

2 ¡ 4 ¡

MAX_QUEUE ¡-­‑1 ¡

0 ¡

1 ¡

2 ¡

3 ¡

Front ¡ Back ¡ items ¡

CS200 Algorithms and Data Structures Colorado State University

Im mp pl le em me en nt ti in ng g q qu ue eu ue e w wi it th h A Ar rr ra ay y

Queue is empty if back is less than front Inserting an item

– Initially front is 0, back is -1 – Increment back – Place new item in items[back]

Queue will be full if back equals MAX_QUEUE - 1

26 CS200 Algorithms and Data Structures Colorado State University

Im mp pl le em me en nt ti in ng g q qu ue eu ue e w wi it th h A Ar rr ra ay y

Removing an item

– Remove item from items[front] – Increment front

Rightward drift

– After a sequence of additions and removals, items in the queue will drift toward the end

  • f the array

– back can reach MAX_QUEUE-1 even when the queue contains only few items.

27 CS200 Algorithms and Data Structures Colorado State University

So

  • l

lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro

  • b

bl le em m ( (1 1) )

Shifting array elements to the left. Therefore front is always pointing to items[0]. Cost of implementation

28 CS200 Algorithms and Data Structures Colorado State University

So

  • l

lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro

  • b

bl le em m ( (2 2) )

Circular implementation of a queue

29

1 ¡ 6 ¡ 5 ¡ 4 ¡ 3 ¡ 2 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 1 ¡ 2 ¡ 4 ¡ 7 ¡

F R O N T ¡ BACK ¡

CS200 Algorithms and Data Structures Colorado State University

So

  • l

lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro

  • b

bl le em m ( (2 2) )

Delete

30

1 ¡ 6 ¡ 5 ¡ 4 ¡ 3 ¡ 2 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 1 ¡ 2 ¡ 4 ¡ 7 ¡

F R O N T ¡ BACK ¡

slide-5
SLIDE 5

1/22/13 ¡ 5 ¡

CS200 Algorithms and Data Structures Colorado State University

So

  • l

lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro

  • b

bl le em m ( (2 2) )

Delete

31

1 ¡ 6 ¡ 5 ¡ 4 ¡ 3 ¡ 2 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 1 ¡ 4 ¡ 7 ¡

BACK ¡ F R O N T ¡

CS200 Algorithms and Data Structures Colorado State University

So

  • l

lv vi in ng g R Ri ig gh ht tw wa ar rd d D Dr ri if ft t P Pr ro

  • b

bl le em m ( (2 2) )

Insert 9

32

1 ¡ 6 ¡ 5 ¡ 4 ¡ 3 ¡ 2 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 1 ¡ 7 ¡

BACK ¡ FRONT ¡

9 ¡

BACK ¡

When ¡either ¡front ¡or ¡back ¡ advances ¡past ¡MAX_QUEUE-­‑1, ¡ it ¡wraps ¡around ¡0 ¡

CS200 Algorithms and Data Structures Colorado State University

Qu ue eu ue e w wi it th h S Si in ng gl le e I It te em m

Queue contains only one item. back and front are pointing at the same slot.

33

1 ¡ 6 ¡ 5 ¡ 4 ¡ 3 ¡ 2 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 1 ¡ 7 ¡

FRONT ¡

9 ¡

BACK ¡ FRONT ¡

CS200 Algorithms and Data Structures Colorado State University

Qu ue eu ue e w wi it th h S Si in ng gl le e I It te em m

The last item is removed. Queue is empty.

– front passed back.

34

1 ¡ 6 ¡ 5 ¡ 4 ¡ 3 ¡ 2 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 9 ¡

BACK ¡ FRONT ¡ FRONT ¡

CS200 Algorithms and Data Structures Colorado State University

In ns se er rt t t th he e l la as st t i it te em m

back catches up to front when the queue becomes full.

35

1 ¡ 6 ¡ 5 ¡ 4 ¡ 3 ¡ 2 ¡ 0 ¡ MAX_QUEUE-­‑1 ¡ 9 ¡

BACK ¡ FRONT ¡

7 ¡ 2 ¡ 6 ¡ 4 ¡ 8 ¡ 1 ¡ 0 ¡

BACK ¡

When ¡the ¡queue ¡is ¡FULL ¡ front ¡is ¡one ¡slot ¡ahead ¡ back ¡as ¡well. ¡ ¡ Keep ¡the ¡count ¡of ¡items ¡and ¡ ¡ Check ¡if ¡the ¡count ¡is ¡equal ¡to ¡ ¡ MAX_QUEUE ¡

CS200 Algorithms and Data Structures Colorado State University

Wr ra ap pp pi in ng g t th he e v va al lu ue es s f fo

  • r

r f fr ro

  • n

nt t a an nd d b ba ac ck k

Initializing

– Front = 0 – Back = MAX_QUEUE-1 – Count = 0

Adding back = (back+1) % MAX_QUEUE; items[back] = newItem; ++count; Deleting deleteItem = items[front]; front = (front +1) % MAX_QUEUE;

  • -count;

36

slide-6
SLIDE 6

1/22/13 ¡ 6 ¡

CS200 Algorithms and Data Structures Colorado State University

enqueue w wi it th h A Ar rr ra ay y

public void enqueue(Object newItem) throws QueueException{ if (!isFull()){ back = (back+1) % (MAX_QUEUE); items[back] = newItem; ++count; }else { throw QueueException(your_message); } }

37 CS200 Algorithms and Data Structures Colorado State University

dequeue() )

public Object dequeue() throws QueueException{ if (!isEmpty()){ Object queueFront = items[front]; front = (front+1) % (MAX_QUEUE);

  • -count;

return queueFront; }else{ throw new QueueException (your_message); } }

38 CS200 Algorithms and Data Structures Colorado State University

Im mp pl le em me en nt ta at ti io

  • n

n w wi it th h L Li is st t

You can implement operation dequeue() as the list operation remove(0). peek() as get(0) enqueue() as add (size()-1, newItem)

39

2 ¡ ¡4 ¡ ¡1 ¡ ¡7 ¡

Front ¡of ¡queue ¡ Back ¡of ¡queue ¡ Posion ¡in ¡the ¡list ¡

0 ¡ ¡1 ¡ ¡2 ¡ ¡3 ¡ ¡

CS200 Algorithms and Data Structures Colorado State University

Ou ut tl li in ne e

Queue? Implementing Queue

Comparison implementations

40 CS200 Algorithms and Data Structures Colorado State University

java.util.Queue

extends java.util.Collection add(e), remove(), element()

  • ffer(e), poll(), peek()

java.util.Deque:

– Subinterface of queue – A linear collection that supports element insertion and removal at both ends.

41 CS200 Algorithms and Data Structures Colorado State University

Su um mm ma ar ry y

  • f

f Q Qu ue eu ue e O Op pe er ra at ti io

  • n

ns s ( (F FI IF FO O) )

ADT ¡Queue ¡ JCF ¡Queue ¡ JCF ¡Deque ¡

enqueue(e) add(e) addLast(e)

  • ffer(e)
  • fferLast(e)

dequeue() remove() removeFirst() poll() pollFirst() peek() peek() peekFirst() element() getFirst()

42

slide-7
SLIDE 7

1/22/13 ¡ 7 ¡

CS200 Algorithms and Data Structures Colorado State University

Su um mm ma ar ry y

  • f

f L LI IF FO O

  • p

pe er ra at ti io

  • n

ns s

ADT Stack JCF Stack JCF Deque push(e) push(e) addFirst(e) pop() pop() removeFirst() peek() peek() peekFirst()

43 CS200 Algorithms and Data Structures Colorado State University

Su um mm ma ar ry y

  • f

f P Po

  • s

si it ti io

  • n

n-

  • O

Or ri ie en nt te ed d A AD DT Ts s

Stack, queue, and list (so far) createStack and createQueue isEmpty for stack and queue push and enqueue pop and dequeue Stack peek and queue peek

44