ECE 242 Data Structures Lecture 5 Queues September 18, 2009 - - PDF document

ece 242 data structures
SMART_READER_LITE
LIVE PREVIEW

ECE 242 Data Structures Lecture 5 Queues September 18, 2009 - - PDF document

ECE 242 Data Structures Lecture 5 Queues September 18, 2009 ECE242 L5: Queues Overview Problem: How can I build a first-in, first-out structure Think about standing in line at the store Queue can grow quite long -


slide-1
SLIDE 1

ECE242 L5: Queues September 18, 2009

ECE 242 Data Structures

Lecture 5

Queues

ECE242 L5: Queues September 18, 2009

Overview °Problem: How can I build a “first-in, first-out” structure

  • Think about standing in line at the store
  • “Queue” can grow quite long
  • Only one person enters at a time
  • Only one person exits at a time

°Queues: frequent usage in programs

  • Who got there first
  • Everyone gets treated the same

°Somewhat easier to understand than stacks but more difficult to implement in Java

slide-2
SLIDE 2

ECE242 L5: Queues September 18, 2009

Queue °Queue: First In First Out (FIFO) °Toll Station

  • Car comes, pays, leaves

°Check-out in Big Y market

  • Customer comes, checks out and leaves

°More examples: Printer, Office Hours, … A B C D

Output Input

ECE242 L5: Queues September 18, 2009

What Is A Queue? °Queue is an abstract data type °Adding an entry at the tail °Deleting an entry at the head

front back Adding Deleting

A B C

slide-3
SLIDE 3

ECE242 L5: Queues September 18, 2009

Abstract Data Types °Queue

  • Operating on both ends
  • Operations: EnQueue(in), DeQueue(out)

°Not possible to add/delete in middle of queue

front back enqueue dequeue

A B C

ECE242 L5: Queues September 18, 2009

Applications of Queue ° Printing Job Management ° Packet Forwarding in Routers ° Message queue in Windows ° I/O buffer

Note that we haven’t specified a size or an implementation

slide-4
SLIDE 4

ECE242 L5: Queues September 18, 2009

Example: Printing Job Management °Many users send their printing jobs to ECS public printer °Printer will put them into a queue according to the arrival time and print the jobs one by one °These printing documents are A.doc, B.doc, C.doc and D.doc °Other applications

  • Packet Forwarding in Routers
  • Message queue in Windows
  • I/O buffer

ECE242 L5: Queues September 18, 2009

Printing Queue A B C Now printing A.doc B C A.doc is finished. Now printing B.doc B C Now still printing B.doc

D.doc comes

D °A.doc B.doc C.doc arrive to printer. C D D B.doc is finished. Now printing C.doc C.doc is finished. Now printing D.doc

slide-5
SLIDE 5

ECE242 L5: Queues September 18, 2009

First-in First-out (FIFO)

items in the same order. When we enqueue entries in the queue and then dequeue them one by one, we will get the items in the same order.

A B C A A B B C C

The first one enqueued is the first

  • ne dequeued.

(FIFO)

A, B, C come in A, B, C come out

ECE242 L5: Queues September 18, 2009

Question °Queue is an abstract data structure °Item can be Integer, Double, String, Employee, Faculty… °How to implement a general queue for all those types?

slide-6
SLIDE 6

ECE242 L5: Queues September 18, 2009

Abstract Data Type

°Same as Stack, we use Object data type instead of int or double or String or other data type °Use an array in queue, which stores all items come in.

  • Object Queue[ ];

°Other implementations are possible

ECE242 L5: Queues September 18, 2009

Array Implementation of Queue B A D C Max_Size rear front

After A leaves,

B D C Max_Size rear front

1 n-1 3 2 1 n-1 3 2

slide-7
SLIDE 7

ECE242 L5: Queues September 18, 2009

Operations

° enqueue

  • add a new item at the rear

° dequeue

  • remove a item from the front

° isEmpty

  • check whether the queue is empty or not

° isFull

  • check whether the queue is full or not

° size

  • return the number of items in the queue

° peek

  • return the front item

ECE242 L5: Queues September 18, 2009

Problem °An array has limited size, once back is at the end of this array, and there is new item coming in, what can we do? Y X……

1 n-1 3 2

back front

slide-8
SLIDE 8

ECE242 L5: Queues September 18, 2009

Two Solutions °Shifting all items to front in the array when dequeue operation. ( Too Costly… ) °Wrapped around array ---- Circular Array B A …… C

1 n-1 3 2

back=3 front=0 C B ……

1 n-1 3 2

back=2 front=0

A leaves

ECE242 L5: Queues September 18, 2009

Circular Array °Wrapped around array B A …… C

1 n-1 3 2

back=3 front=0 A B C 2 3 n-1 1 back=3 front=0

slide-9
SLIDE 9

ECE242 L5: Queues September 18, 2009

EnQueue & DeQueue In Circular Array ° EnQueue

  • back = (back + 1) MOD n

A B C 2 3 n-1 1 back=3

  • DeQueue

– front = (front + 1) MOD n

B C 2 3 n-1 1 front=1

ECE242 L5: Queues September 18, 2009

Empty/Full In Circular Array °When rear equals front, Queue is empty °When (rear + 1) MOD n equals front, Queue is full °Circular array with capacity n at most can hold n-1 items.

slide-10
SLIDE 10

ECE242 L5: Queues September 18, 2009

Summary ° Queues are often implemented via interfaces

  • Hides low level implementations

° Queues can be tricky to implement

  • Usually implemented with arrays

° Queues useful for many applications

  • Allows uniform access

° Next time: More queues