4 Queues
4 Queues
Queues
1
Phenomena on the computer
In Game, when factory produce units
See online movies
The way printer works
2
Objective
Queue Abstract Data Type
Sequential Allocation
Linked Allocation
Applications
Priority Queues
3
Queue
Queue is a list with the restriction that insertions
are performed at one end and deletions are
performed at the other end of the list
Also known as: First-in-first-out (FIFO) list
4
ADT of Queue
Value:
A sequence of items that belong to some data type ITEM_TYPE
Operations on q:
1. Boolean IsEmpty()
Postcondition: If the queue is empty, return true, otherwise return false
2. Boolean IsFull()
Postcondition: If the queue is full, return true, otherwise return false
3. ITEM_TYPE Dequeue() /*take out the front one and return its value*/
Precondition: q is not empty
Postcondition: The front item in q is removed from the sequence and returned
4. Void Enqueue(ITEM_TYPE e) /*to append one item to the rear of the queue*/
Precondition: q is not ______
Postcondition: e is added to the sequence as the rear one
5
Implementation of Queue
Sequential Allocation (Using Array)
#define TOTAL_SLOTS 100
class MyQueue
{
private:
int front; //the index of the front slot that contains the front item
int rear; //the index of the first empty slot at the rear of queue
int items[TOTAL_SLOTS];
};
front rear
6
Implementation of Queue
Sequential Allocation (Using Array)
#define TOTAL_SLOTS 100
class MyQueue
{ Slot#0 Slot#1 Slot#2 Slot#3 … Slot#99
private: Item A Item B Item C Empty Empty Empty
int front;
front rear
int rear;
int items[TOTAL_SLOTS];
};
front rear
front rear
9
Implementation of Queue
Sequential Allocation (Using Array)
bool MyQueue::isEmpty()
{
return (front==rear);
}
bool MyQueue::isFull()
{
return((rear+1)%TOTAL_SLOTS==front);
}
NULL
front rear
11
Linked Implementation of Queue
// MyQueue.h
#include “stdlib.h” // MyQueue.cpp
{
class MyQueue #include “MyQueue.h”
{ MyQueue::MyQueue()
public: {
MyQueue( ); size=0;
bool IsEmpty(); front=NULL;
void Enqueue(int ); rear=NULL;
int Dequeue(); }
private: bool MyQueue::IsEmpty()
ListNode* front; {
ListNode* rear; return (front==NULL);
int size; }
};
}
12
Linked Implementation of Queue
To insert an item (Enqueue) Step 1: Allocate a new slot, p, to store the item.
We have 2 cases: Step 2: Connect p to the queue (2 cases).
The queue is empty or not. Step 3: Update the rear pointer to point to p.
front=NULL
New Item A … Item X NULL New
rear=NULL
front rear
front rear
front rear
13
Linked Implementation of Queue
To insert an item (Enqueue) Step 1: Allocate a new slot, p, to store the item.
We have 2 cases: Step 2: Connect p to the queue (2 cases).
The queue is empty or not. Step 3: Update the pRear pointer to point to p.
// MyQueue.cpp
#include “MyQueue.h”
void MyQueue::Enqueue(int data)
{
ListNode *p=new ListNode(data);
if (IsEmpty())
front=p;
else
rear->next=p;
rear=p;
}
14
Linked Implementation of Queue
To delete an item (the front item) and return it
We have 3 cases:
The queue has 0 item, 1 item or more than one item.
// MyQueue.cpp
#include “MyQueue.h”
int MyQueue::Dequeue()
{
int ret_value;
if (!IsEmpty())
{
ret_value=front->data;
front=front->next;
if(front==NULL)
rear=NULL;
}
return ret_value; 16
}
Application 1: Reversing a Stack
stack
Reversing a stack
D
Stack *s; C Pop from stack and
B insert into a queue
Queue *p; A
…
while(!s->IsEmpty())
{ x = s->pop(); queue
p->Enqueue(x); D C B A
} empty
while (!p->IsEmpty())
{ x = p->Dequeue();
s->push(x); A
} B
C Delete from queue and
D Push onto stack 17
Application 2: Phenomena on the computer
18
Round Robin Schedule
job 1 : 4 time slots; job 2 : 3 time slots
job 3 : 1 time slot; job 4 : 2 time slots
19
Queue enough?
In Game, when factory produce units
Suppose a factory can produce the following
three units:
Attacker Least important
Defender Most important
Worker Moderately important
When you are giving commands, you do not
have so much time to worry about the order of
production. It should be AI’s work to arrange
that for you
20
Priority Queue
Priority Queue
The elements in a stack or a FIFO queue are ordered based on
the sequence in which they have been inserted.
In a priority queue, the sequence in which elements are
removed is based on the priority of the elements.
21
Priority Queue
Priority Queue - List Implementation
To implement a priority queue as an ordered list.
22
Priority Queue
Priority Queue - List Implementation
To implement a priority queue as an unordered list.
Deletion: Traverse the entire list to find the maximum priority element.
O(n).
Copy the value of the element to return it later. O(1)
Delete the node. O(1)
Altogether: O(n)
24