ece 242 data structures
play

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 -


  1. 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 - 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 September 18, 2009 ECE242 L5: Queues

  2. Queue ° Queue: First In First Out (FIFO) Output Input D C B A ° Toll Station • Car comes, pays, leaves ° Check-out in Big Y market • Customer comes, checks out and leaves ° More examples: Printer, Office Hours, … September 18, 2009 ECE242 L5: Queues What Is A Queue? ° Queue is an abstract data type ° Adding an entry at the tail ° Deleting an entry at the head Adding Deleting C B A front back September 18, 2009 ECE242 L5: Queues

  3. Abstract Data Types ° Queue • Operating on both ends • Operations: EnQueue(in), DeQueue(out) ° Not possible to add/delete in middle of queue enqueue dequeue C B A back front September 18, 2009 ECE242 L5: Queues 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 September 18, 2009 ECE242 L5: Queues

  4. 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 September 18, 2009 ECE242 L5: Queues Printing Queue ° A.doc B.doc C.doc arrive to printer. Now printing A.doc C B A C B A.doc is finished. Now printing B.doc D.doc comes D C B Now still printing B.doc D C B.doc is finished. Now printing C.doc D C.doc is finished. Now printing D.doc September 18, 2009 ECE242 L5: Queues

  5. First-in First-out (FIFO) A, B, C come in The first one enqueued is the first C B one dequeued. (FIFO) A C When we enqueue entries in the queue and B A then dequeue them one by one, we will get the A, B, C come out items in the same order. items in the same order. C B A September 18, 2009 ECE242 L5: Queues 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? September 18, 2009 ECE242 L5: Queues

  6. 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 September 18, 2009 ECE242 L5: Queues Array Implementation of Queue n-1 3 2 1 0 D C B A rear front Max_Size After A leaves, n-1 3 2 1 0 D C B rear front Max_Size September 18, 2009 ECE242 L5: Queues

  7. 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 September 18, 2009 ECE242 L5: Queues 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? n-1 3 2 1 0 Y X…… front back September 18, 2009 ECE242 L5: Queues

  8. Two Solutions ° Shifting all items to front in the array when dequeue operation. ( Too Costly… ) n-1 3 2 1 0 n-1 3 2 1 0 A leaves …… C B A …… C B back=3 front=0 back=2 front=0 ° Wrapped around array ---- Circular Array September 18, 2009 ECE242 L5: Queues Circular Array ° Wrapped around array back=3 3 2 C 1 front=0 n-1 3 2 1 0 B …… C B A 0 A n-1 back=3 front=0 September 18, 2009 ECE242 L5: Queues

  9. EnQueue & DeQueue In Circular Array ° EnQueue • DeQueue • back = (back + 1) MOD n – front = (front + 1) MOD n back=3 front=1 3 3 2 2 C C 1 1 B B 0 A 0 n-1 n-1 September 18, 2009 ECE242 L5: Queues 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. September 18, 2009 ECE242 L5: Queues

  10. 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 September 18, 2009 ECE242 L5: Queues

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend