0% found this document useful (0 votes)
40 views24 pages

4 Queues

The document discusses queues and their implementation using data structures. A queue is a first-in, first-out (FIFO) list where items can only be added at the rear and removed from the front. Queues can be implemented sequentially using arrays, with pointers tracking the front and rear. They can also be implemented using linked lists, with front and rear pointers connecting the nodes. Operations like enqueue and dequeue are demonstrated for both implementations.

Uploaded by

Dickson Wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views24 pages

4 Queues

The document discusses queues and their implementation using data structures. A queue is a first-in, first-out (FIFO) list where items can only be added at the rear and removed from the front. Queues can be implemented sequentially using arrays, with pointers tracking the front and rear. They can also be implemented using linked lists, with front and rear pointers connecting the nodes. Operations like enqueue and dequeue are demonstrated for both implementations.

Uploaded by

Dickson Wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures

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];
};

Slot#0 Slot#1 Slot#2 … Slot#98 Slot#99

Suppose some items are appended into the queue:


Slot#0 Slot#1 Slot#2 Slot#3 … Slot#99
Item A Item B Item C Empty Empty Empty

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];
};

Suppose we remove Slot#0 Slot#1 Slot#2 Slot#3 … Slot#99


2 items: Empty Empty Item C Empty Empty Empty

front rear

Then we remove the Slot#0 Slot#1 Slot#2 Slot#3 … Slot#99


remaining one item: Empty Empty Empty Empty Empty Empty

front rear

If the queue is empty, we’ll have___________.


7
#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];
};

Suppose 2 items Slot#0 Slot#1 Slot#2 Slot#3 Slot#4 … Slot#98 Slot#99


are removed and Empty Empty Item C Item D Item E Item XX Empty
96 items added:
front rear

Then, we add Slot#0 Slot#1 Slot#2 Slot#3 Slot#4 … Slot#98 Slot#99


(append) item Empty Empty Item C Item D Item E Item XX Item YY
YY:
rear front
Then, we add Slot#0 Slot#1 Slot#2 Slot#3 Slot#4 … Slot#98 Slot#99
(append) one Item ZZ Empty Item C Item D Item E Item XX Item YY
more item ZZ:
rear front
However, we can’t add further item. Reason: we should not let rear = front if the
queue is not empty. (The queue is empty when rear = front) 8

Hence, this implementation allows only “______________________” items.


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];
public:
bool isEmpty();
bool isFull();
void enqueue(int );
int dequeue();
};

9
Implementation of Queue
Sequential Allocation (Using Array)
bool MyQueue::isEmpty()
{
return (front==rear);
}
bool MyQueue::isFull()
{
return((rear+1)%TOTAL_SLOTS==front);
}

void MyQueue::enqueue(int data) int MyQueue::dequeue( )


{ {
if(!isFull()) int ret_val;
{ if(!isEmpty())
items[rear]=data; {
rear=(rear+1) %TOTAL_SLOTS; ret_val=items[front];
} front=(front+1)%TOTAL_SLOTS;
} return ret_val;
} 10
}
Implementation of Queue
Using Linked List

NULL

front rear

 Queue can also be implemented with linked list.


 A pointer front points to the first node of the queue.
 A pointer rear points to the last node of the queue.
 If the queue is empty, then front=rear=NULL.
 When will the queue be full?

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.

Case 1: The queue is empty Case 2: The queue is not empty

front=NULL
New Item A … Item X NULL New
rear=NULL

front rear

New NULL Item A … Item X New NULL

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.

Case 1: The queue has 0 item  Output error Value of


Item A
Case 2: The queue has 1 item
front=NULL
Item A NULL Item A NULL
rear=NULL
front rear front rear
Case 3: The queue has more than one item
Value of
Item A
Item A Item B … Item X NULL Item A Item B … Item X NULL
front rear front rear
15
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

See online movies


The way printer works
Round Robin Schedule
Establish a queue for current jobs
Whenever a time slot is used up
Insert the current job into the queue
Begin executing the job fetched from the queue

18
Round Robin Schedule
 job 1 : 4 time slots; job 2 : 3 time slots
 job 3 : 1 time slot; job 4 : 2 time slots

1(4 left) 2(3 left) 3(1 left) 4(2 left)


2(3 left) 3(1 left) 4(2 left) 1(3 left)
3(1 left) 4(2 left) 1(3 left) 2(2 left)
4(2 left) 1(3 left) 2(2 left)
1(3 left) 2(2 left) 4(1 left)
2(2 left) 4(1 left) 1(2 left)
2(1left) 4(1 left) 1(2 left)
2(1left) 1(2 left)
2(1left) 1(1 left)
1(1 left)

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.

Ordered Priority Queue A B C D


Priority=1 Priority=2 Priority=3 Priority=3
(highest priority) (lowest priority)

The first element to be removed.

Unordered Priority Queue B C A D


Priority=2 Priority=3 Priority=1 Priority=3

21
Priority Queue
Priority Queue - List Implementation
 To implement a priority queue as an ordered list.

Time complexity of the operations :


(assume the sorting order is from highest priority to lowest)

Insertion: Find the location of insertion. O(n)


Link the element at the found location. O(1)
Altogether: O(n)

Deletion: The highest priority element is at the front.


ie. Remove the front element takes O(1) time

22
Priority Queue
Priority Queue - List Implementation
 To implement a priority queue as an unordered list.

Time complexity of the operations :

Insertion: Simply insert the item at the rear. O(1)

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)

We will come back to this after we learned trees


23
Learning Objectives
1. Explain the concepts of Queue
2. Understand the two functions of Queue
3. Able to solve simple applications using
Queue
4. Understand the concept of Priority
Queue and its two implementations

D:1; C:1,2; B:1,2,3; A:1,2,3,4

24

You might also like