1 / 26

Queues

Data Structure: Chapter 4. Min Chen School of Computer Science and Engineering Seoul National University. Queues. Content. Definition of Queues Operators for Queues Insert Remove Peek Special Queues Circular Queues Priority Queues Efficiency of Queues. Definition of Stacks.

keaira
Télécharger la présentation

Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure: Chapter 4 Min Chen School of Computer Science and Engineering Seoul National University Queues

  2. Content • Definition of Queues • Operators for Queues • Insert • Remove • Peek • Special Queues • Circular Queues • Priority Queues • Efficiency of Queues

  3. Definition of Stacks • A Queue is a First-In-First-Out(FIFO) abstract data structure. • In a Queue, new item insert to the rear and the item in the front will be removed first Front Rear

  4. Functional Requirements of a Queue • A queue is similar to a stack, except that you join the queue at one end and leave it at the other. • We shall again use an array to storing the items in the queue. • We shall need two pointers, one to indicate where the next item to be placed in the queue should be stored (rear), and another to indicate the location of the next item to be retrieved (front).

  5. Analogy of Queue Fig.1 Bus Queue: A Typical Queue

  6. Analogy of Queue: Some everyday queues

  7. Typical Java Interface for a queue of objects public interface QueueInterface{ /** Task: Add item to back */ public void enqueue(Object newEntry); /** Task: Removes and returns the front */public Object dequeue(); /** Task: Retrieves front of queue without removing */public Object getFront(); /** Task: Determines whether the queue is empty.*/public booleanisEmpty(); /** Task: Removes all entries from the queue. */public void clear();} Abstract Data Type (ADT): don’t care about how to realize, just use it.

  8. A Simple Example Queue of strings after (a) enqueueadds Jim; (b) Jess; (c) Jill; (d) Jane; (e) Joe; (f) dequeue retrieves, removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves, removes Jess.

  9. Using a Queue to Simulate a Waiting Line

  10. Using a Queue to Simulate a Waiting Line: Customer Enqueues for Service

  11. Using a Queue to Simulate a Waiting Line: Customer Enqueues for Service 2

  12. Types of Queuing Systems • Single queue, single server • Single queue, multiple servers: • Improve processes, e.g., most banks use a snake queue instead individual lines at each teller. • 2 queues, 2 servers vs. 1 queue, 2 servers • Increase utilization ratio • Electronic Queuing System (see animation) • Multiple queues, single server • Intersection Management byTraffic Light (see animation) • Multiple queues, multiple servers

  13. Inpatient customer strategies • Customers who intended to join the line but don’t • Customers who join, then leave • Jumping from queue to queue • Jumping to newly opened line • Follow the employee • Two line strategy (bring spouse or kids), then merge into a faster queue See Animation

  14. Why Study Queues • People hate to wait in queues – shorter queues may result in increased efficiency. • Some customers leave shopping carts full of groceries at the checkout counters and frozen and cold food are wasted.

  15. Operators for Queues: Insert • Insert: insert a data item to the rear of the queue Rear Front Fig.2 Insert Operator in Queues

  16. Operators for Queues: Remove • Remove: Remove a data item at the front of the queue Rear Program Front Fig.3 Remove Operator in Queues

  17. Operators for Queues: Peek • Peek: Get a data item at the front of the queue without removing Rear Program Front Fig.4 Peek Operator in Queues

  18. Special Queues 1: Circular Queues • Circular Queues • We shall treat the underlying array as a circular buffer. • Problem Statement Rear Rear Front No Space Fig.5 Insertion of Circular Queues

  19. Circular Queues: Simple Implementation • Items to be enqueued will be written in the array at queue[rear]. • Items to be dequeued will be read from queue[front]. • After each writing, top is incremented by one. • After each reading, rear is incremented by one. • Incrementing of rear and front is always modulo queue.length.

  20. Special Queues 2: Priority Queue • The priority queue is a data structure that only allows access to the “minimum” item in a set.

  21. Priority Queue: Realization of Network Simulation • Discrete event simulation (desired simulation time is used to set priorities)

  22. Special Queues 2: Priority Queue • The items in Priority Queue has priority • Insertion in Priority Queue is different 81 Rear 214 99 92 75 45 21 112 Front Fig.6 Insertion of Priority Queues

  23. Implementing Priority Queues • Goals: fast findMin, deleteMin, insert • findMin: Identify entry whose key is lowest • deleteMin: remove entry whose key is lowest • Insert: any entry may be inserted at any time • How should we implement a priority queue? • Option 1: a linked list • Option 2: a sorted linked list • Option 3: a binary search tree • Option 4: a balanced binary search tree

  24. Problem rised by Priority Queue • Starving • When items with higher priority are inserted into the queue, the items with lower priority will be starved • Fairness problems should be considered

  25. Efficiency of Queues • In conventional queues, items can be both pushed and popped from a stack in constant O(1) time • For priority queues, insertion runs in O(N) time, whereas deletion takes O(1) time

  26. Thank you!

More Related