Queue Definition Ordered list with property: – All insertions take place at one end (tail) – All deletions take place at other end (head) Queue: Q = (a 0 , a 1 , …, a n-1 ) a0 is the front element, a n-1 is the tail, and a i is behind a i-1 for all i, 1 <= i < n
24
Embed
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Queue Definition
Ordered list with property:– All insertions take place at one end (tail)– All deletions take place at other end (head)
Queue: Q = (a0, a1, …, an-1)
a0 is the front element, an-1 is the tail, and ai is behind ai-1 for all i, 1 <= i < n
if (IsEmpty()) {QueueEmpty(); return 0};else { head = head + 1; item = queue[head]; return
&item; }}
Example: OS Job Scheduling
OS has to manage how jobs (programs) are executed on the processor – 2 typical techniques:-Priority based: Some ordering over of jobs based on importance
(Professor X’s jobs should be allowed to run first over Professor Y).-Queue based: Equal priority, schedule in first in first out order.
Queue Based Job Processing
Front Rear Q[0] Q[1] Q[2] Q[3] Comments
-1 -1 Initial
-1 0 J1 Job 1 Enters
-1 1 J1 J2 Job 2 Enters
-1 2 J1 J2 J3 Job 3 Enters
0 2 J2 J3 Job 1 Leaves
0 3 J2 J3 J4 Job 4 Enters
1 3 J3 J4 Job 2 Leaves
Job Processing
• When J4 enters the queue, rear is updated to 3.• When rear is 3 in a 4-entry queue, run out of space.• The array may not really be full though, if head is
not -1.• Head can be > -1 if items have been removed from
queue.
Possible Solution: When rear = (maxSize – 1) attempt to shift data forwards into empty spaces and then do Add.
Queue Shift
private void shiftQueue(KeyType* queue, int & head, int & tail)
{int difference = head – (-1); // head + 1for (int j = head + 1; j < maxSize; j++){
// Create a new node that contains data y// Has to go at end// Set current rear link to new node pointer// Set new rear pointer to new node pointerrear = rear->link = new QueueNode(y, 0);