Title: Queues
1Queues
2Queue 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 lt i lt n
3Queue Definition
- Because of insertion and deletion properties,
- Queue is very similar to
- Line at the grocery store
- Cars in traffic
- Network packets
- .
- Also called first-in first out lists
4Queue Implementation Ideas
- Container type class for holding data
- Array versus Linked List? Whos better?
- Head index pointer
- Position right before first element in queue
- Tail index pointer
- Position of last element in queue
-
5Array-Based Queue Definition
- template ltclass KeyTypegt
- class Queue
-
- public
- Queue(int MaxQueueSize DefaultSize)
- Queue()
- bool IsFull()
- bool IsEmpty()
- void Add(const KeyType item)
- KeyType Delete(KeyType item)
-
- private
- void QueueFull() // error handling
- void QueueEmpty() // error handling
- int head, tail
- KeyType queue
- int MaxSize
6Queue Implementation
- Constructor
- template ltclass KeyTypegt
- QueueltKeyTypegtQueue(int MaxQueueSize)
MaxSize(MaxQueueSize) -
- queue new KeyTypeMaxSize
- head tail -1
-
7Queue Implementation
- Destructor
- template ltclass KeyTypegt
- QueueltKeyTypegtQueue()
-
- delete queue
- head tail -1
-
8Queue Implementation
- IsFull() and IsEmpty()
- template ltclass KeyTypegt
- bool QueueltKeyTypegtIsFull()
-
- return (tail (MaxSize-1))
-
- template ltclass KeyTypegt
- bool QueueltKeyTypegtIsEmpty()
-
- return (head tail)
-
9Queue Implementation
- Add() and Delete()
- template ltclass KeyTypegt
- void QueueltKeyTypegtAdd (const KeyType item)
-
- if (IsFull()) QueueFull() return
- else tail tail 1 queuetail item
-
- template ltclass KeyTypegt
- KeyType QueueltKeyTypegtDelete(KeyType item)
-
-
- if (IsEmpty()) QueueEmpty() return 0
- else head head 1 item queuehead
return item -
10Example 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 Xs jobs should be allowed to run
first over Professor Y). - -Queue based Equal priority, schedule in first
in first out order.
11Queue Based Job Processing
Front Rear Q0 Q1 Q2 Q3 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
MaxSize 4
12Job 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 gt -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.
13Queue Shift
- private void shiftQueue(KeyType queue, int
head, int tail) -
- int difference head (-1) // head 1
- for (int j head 1 j lt maxSize j)
-
- queuej-difference queuej
-
- head -1
- tail tail difference
14Queue Shift
- Worst Case For Queue Shift
- Full Queue
- Alternating Delete and Add statements
Front Rear Q0 Q1 Q2 Q3 Comments
-1 3 J1 J2 J3 J4 Initial
0 3 J2 J3 J4 Job 1 Leaves
-1 3 J2 J3 J4 J5 Job 5 Enters
0 3 J3 J4 J5 Job 2 Enters
-1 3 J3 J4 J5 J6 Job 6 Leaves
15Worst Case Queue Shift
- Worst Case
- Shift entire queue Cost of O(n)
- Do every time perform an add
- Too expensive to be useful
- Worst case is not that unlikely, so this suggests
finding an alternative implementation.
16Circular Array Implementation
- Basic Idea Allow the queue to wrap-around
- Implement with addition mod size
- tail (tail 1) queueSize
4
4
J4
3
3
J3
J2
J1
2
2
N-2
J1
J2
N-2
J3
1
N-1
1
N-1
0
0
17Linked Queues
- Problems with implementing queues on top of
arrays - Sizing problems (bounds, clumsy resizing, )
- Non-circular Array Data movement problem
- Now that have the concepts of list nodes, can
take advantage of to represent queues. - Need to determine appropriate way of
- Representing front and rear
- Facilitating node addition and deletion at the
ends.
18Linked Queues
Add(Hat) Add(Mat) Add(Cat) Delete()
19Linked Queues
- Class QueueNode
- friend class Queue
- public
- QueueNode(int d, QueueNode l)
- private
- int data
- QueueNode link
-
20Linked Queues
- class Queue
-
- public
- Queue()
- Queue()
- void Add(const int)
- int Delete(int)
- bool isEmpty()
- private
- QueueNode front
- QueueNode rear
- void QueueEmpty()
-
21Linked Queues
- QueueQueue()
-
- front 0
- rear 0
-
- bool QueueisEmpty()
-
- return (front 0)
-
0
0
22Linked Queues
- void QueueAdd(const int y)
-
- // 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 pointer
- rear rear-gtlink new QueueNode(y, 0)
-
MAT
23Linked Queues
- int QueueDelete(int retValue)
-
- // handle empty case
- if (isEmpty()) QueueEmpty() return 0
- QueueNode toDelete front
- retValue toDelete.data
- front toDelete-gtlink
- delete toDelete
- return retValue
-
returnValue
HAT
MAT
24Queue Destructor
- Queue destructor needs to remove all nodes from
head to tail.
MAT
if (front) QueueNode temp while (front !
rear) temp front front front -gt
link delete temp delete front front rear
0
0
0
25Front vs Delete
- Implementation as written has to remove the item
from the queue to read data value. - Some implementations provide two separate
functions - Front() which returns the data in the first
element - Delete() which removes the first element from the
queue, without returning a value.