Date post: | 16-Dec-2015 |
Category: |
Documents |
Upload: | rodney-shapley |
View: | 214 times |
Download: | 0 times |
Queues
Min ChenSchool of Computer Science and Engineering Seoul National University
Data Structure: Chapter 4
Content
Definition of Queues Operators for Queues
Insert Remove Peek
Special Queues Circular Queues Priority Queues
Efficiency of Queues
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
Rear
Front
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).
7
Typical Java Interface for a queue of objects
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
A Simple ExampleQueue 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.
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
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 queueSee Animation
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.
Operators for Queues: Insert
Insert: insert a data item to the rear of the queue
Fig.2 Insert Operator in Queues
FrontRear
Program
Operators for Queues: Remove
Remove: Remove a data item at the front of the queue
Fig.3 Remove Operator in Queues
FrontRear
Program
Operators for Queues: Peek
Peek: Get a data item at the front of the queue without removing
Fig.4 Peek Operator in Queues
FrontRear
Special Queues 1: Circular Queues Circular Queues
We shall treat the underlying array as a circular buffer. Problem Statement
FrontRear
No Space
Rear
Fig.5 Insertion of Circular Queues
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.
The priority queue is a data structure that only allows access to the “minimum” item in a set.
Special Queues 2: Priority Queue
Priority Queue: Realization of Network Simulation
Time
Simulation Kernel
Intrpt Code
Module ID
Event ID
Event Type Discrete event
simulation (desired simulation time is used to set priorities)
Special Queues 2: Priority Queue
The items in Priority Queue has priority
Insertion in Priority Queue is different
Fig.6 Insertion of Priority Queues
FrontRear
2199 45
81
7592112214
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
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
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