In The Name of Allah The Most Beneficent The Most Merciful: Subject: Data Structures & Algorithms
In The Name of Allah The Most Beneficent The Most Merciful: Subject: Data Structures & Algorithms
Lecture : 14
AL 2
Priority Queue
Priority Queue is an extension of queue with
following properties.
3.If two elements have the same priority, they are served
according to their order in the queue.
AL 3
Priority Queue
Idea behind the Priority Queue is simple:
It is a queue
But the items are ordered by a key.
Implies your ‘position’ in the queue may be
changed by the arrival of a new item.
class PriorityQueue {
public:
PriorityQueue() {
size = 0; rear = -1;
};
~PriorityQueue() {};
int full(void)
{
return ( size == PQMAX ) ? 1 : 0;
};
Priority Queue
Event* remove()
{
if( size > 0 ) {
Event* e = nodes[0];
for(int j=0; j < size-2; j++ )
nodes[j] = nodes[j+1];
return e;
}
return (Event*)NULL;
cout << "remove - queue is empty." << endl;
};
Priority Queue
int insert(Event* e)
{
if( !full() ) {
rear = rear+1;
nodes[rear] = e;
size = size + 1;
sortElements(); // in ascending order
return 1;
}
cout << "insert queue is full." << endl;
return 0;
};
CPU Scheduling
In Simulators
Scheduling the next event (smallest event time)
AL 8
Comparing queue implementations
Big-O Comparison of Queue Operations
Operation Array Linked
Implementation Implementation
Class constructor O(1) O(1)
Front O(1) O(1)
IsFull O(1) O(1)
IsEmpty O(1) O(1)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Destructor O(1) O(N)
Double-ended queue
• A deque, also known as a double-ended queue,
is an ordered collection of items similar to the
queue. It has two ends, a front, and a rear, and
the items remain positioned in the collection.
• In a sense, this hybrid linear structure provides
all the capabilities of stacks and queues in a
single data structure.
AL 12
Implementation of Double ended Queue
• As Stack
– When insertion and deletion is made at the
same side.
• As Queue
– When items are inserted at one end and
removed at other end.
AL 15
Applications of Deque
AL 17