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

## Presentation on theme: "Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4."— Presentation transcript:

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

 Definition of Queues  Operators for Queues  Insert  Remove  Peek  Special Queues  Circular Queues  Priority Queues  Efficiency of Queues

 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 Rear Front

 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).

Fig.1 Bus Queue: A Typical Queue

7 Abstract Data Type (ADT): don’t care about how to realize, just use it. 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 boolean isEmpty(); /** Task: Removes all entries from the queue. */ public void clear(); }

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

 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

 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

 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.

 Insert: insert a data item to the rear of the queue Fig.2 Insert Operator in Queues Front Rear

Program  Remove: Remove a data item at the front of the queue Fig.3 Remove Operator in Queues Front Rear

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

 Circular Queues  We shall treat the underlying array as a circular buffer.  Problem Statement Front Rear No Space Rear Fig.5 Insertion of Circular Queues

 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.

 The priority queue is a data structure that only allows access to the “minimum” item in a set.

 Discrete event simulation (desired simulation time is used to set priorities)

 The items in Priority Queue has priority  Insertion in Priority Queue is different Fig.6 Insertion of Priority Queues Front Rear 219945 81 7592 112 214

 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

 Starving  When items with higher priority are inserted into the queue, the items with lower priority will be starved  Fairness problems should be considered

 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

Download ppt "Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4."

Similar presentations