You are on page 1of 35

DITP

DITP 2113
2113 Data
Data Structure
Structure and
and Algorithm
Algorithm

Lecture
Lecture 10
10 :: Queue
Queue (Part
(Part 2)
2)

Objectives:
To understand circular.
To understand circular linked queue.

1
Introduction
The simplest array implementation starts a queue at the first
position in the array, with things enqueued in increasingly higher
positions.

One integer (last) keeps track of the position of the last thing in the
queue.

Another (first) keeps track of the position of the first thing in the
queue.

When something is dequeued, the position of the first thing is


incremented.

After a series of enqueues and dequeues the portion of the array


that is occupied drifts to increasing large positions, eventually
requiring the array to be "grown".

2
Introduction

The bad thing is that as things are dequeued the beginning positions of
the array are never re-used and become wasted space.

This is NOT a good way to use an array to implement a queue 3


Circular Queue
• In circular queue, the last element will be followed by the
first element. Please refer the diagram below :

0
4

front = -1
rear = -1 1
3
count = 0

2
Circular Queue

front = 0 0
4
Enqueue 30 30
Enqueue 70
Enqueue 40 1
70
3
40

2
count = 3 rear = 2

After enqueue 3 items, the value of front is 0


and the rear value is moved to 2.
Circular Queue

0
4
Dequeue
Dequeue
1
3
40

2
front = 2
rear = 2
count= 1

After 2 items dequeue, the front value will


be moved to 2 and no changes for rear
value.
Circular Queue
rear = 3
0
4
Enqueue 50 100 80
Enqueue 80
Enqueue 100
1
50
Use this formula to find 3
the index of rear. 40

2
rear = (rear + 1) % size front = 2
count = 4

After 3 items is enqueue, the rear value is set


to 0. The variable count is tested to know
when the queue is full.
Example – Class Definition
const int size = 100;
  template <class TYPE>
class Queue
{ private:
TYPE arr[size]; //array
int count; //num of elements
int front; //front queue
int rear; //rear queue
public:
Queue(); //constructor
void Enqueue(TYPE);
void Dequeue();
TYPE queueFront();
TYPE queueRear();
bool Empty();
bool Full(); 8
}; //end class Queue
Example - Constructor
• Constructor is created to make sure that before

the data is enqueue, front and rear will be


positioned at –1. While count = 0.

template <class TYPE>

Queue<TYPE>::Queue()
{
front = -1;
rear = -1;
count = 0;
 
} // end Constructor
9
Enqueue
• Enqueue method is used to enqueue data at rear
of the queue.

• Before Enqueue , make sure the queue is not full.


Else the error message Overflow!!! will be
displayed.

• Enqueue method will make


• rear and count value is added
• rear is set to 0 if it has reach the max array size
• front is set to 0 if queue is empty

 
10
Example - Enqueue
template <class TYPE>
void Queue<TYPE>::Enqueue(TYPE value) {
if (!Full()){ // if queue is not full
rear++;
// test the index to determine the rear
if (rear == size)
rear = 0;
if (count == 0) // if queue is empty
front = 0;
arr[rear] = value;
count++;
}
else
cout<<“Overflow!!!”;
 
} // end Enqueue 11
Example – Enqueue (using formula)
template <class TYPE>
void Queue<TYPE>::Enqueue(TYPE value) {
if (!Full()){ // if queue is not full
rear = (rear + 1) % size;
if (count == 0) // if queue is empty
front = 0;
arr[rear] = value;
count++;
}
else
cout<<“Overflow!!!”;
 
} // end Enqueue

12
Dequeue
• Dequeue method is used to delete or remove data
from the front queue.

• Before Dequeue is done, make sure the queue is


not empty. If the queue is empty, an error message
Underflow!!! will be displayed.

• Dequeue method will cause


• front value is added
• front value will be zero if it has reach the max
array size
• front and rear value will be -1 if there is 1 item
in the queue
• count value will decrease with one
13
Example - Dequeue
template <class TYPE>
void Queue<TYPE>::Dequeue(){
if (Empty()) //if queue is empty
cout<<“Underflow!!!!!”;
else {
front++;
// test the index to determine the front
if (front == size)
front = 0;
if (count == 1)
rear = front = -1;
count--;
}
} //end Dequeue
14
Example – Dequeue
template <class TYPE>
void Queue<TYPE>::Dequeue(){
if (Empty()) //if queue is empty
cout<<“Underflow!!!!!”;
else {
front = (front + 1) % size;
if (count == 1)
rear = front = -1;
count--;
}
} //end Dequeue

15
Example - Empty

• Empty method will check whether the queue is


empty or not.

template <class TYPE>

bool Queue <TYPE> :: Empty()


{
//true if queue is empty
return (count == 0);
} //end Empty

 
16
Example - Full

• Full method will check whether the queue is full or


not.

template <class TYPE>

bool Queue <TYPE> :: Full()


{
//true if queue is full
return (count == size);
} //end Full

 
17
Example – Queue Front
• queueFront will return the value that is located at
the front.

template <class TYPE>

TYPE Queue <TYPE>::queueFront()


{
if (Empty())
return false;
else
return arr[front];
} // end queueFront
 

18
Example – Queue Rear
• queueRear will return the value that is located at
the rear.

template <class TYPE>

TYPE Queue <TYPE>::queueRear()


{
if (Empty())
return false;
else
return arr[rear];
} // end queueRear
 

19
Example – main function
• The code execution:

int main()
{ Queue <int> one; // build object one
one.Enqueue(40); // enqueue 40 into one
one.Enqueue(30); // enqueue 30 into one
one.Enqueue(20); //enqueue 20 into one
cout << “\nFront: "<<one.queueFront();
cout << “\nRear: "<<one.queueRear();
one.Dequeue(); // remove from one
cout << “\nAfter one Dequeue:”;
cout << “\nFront: "<<one.queueFront();
return 0;
}  // end main
 

20
Example – Output
Output:

Front: 40
Rear: 20
After one Dequeue:
Front: 30
 

21
Circular Linked Queue
Physical Diagram for Circular Queue using the
Pointer.

front count rear


N

data next data next data next data next

The difference between the circular linked queue and linked


queue, pointer rear->next will always point to the front.

22
Example – Node
• Definition of Node structure for queue using pointer:

template <class TYPE>

struct Node
{ TYPE data;
Node <TYPE> *next;
};

23
Example – Class Definition
• Definition for class Queue using pointer:
template <class TYPE>
class LinkedQueue
{ private:
Node <TYPE> *front;
int count;
Node <TYPE> *rear;
public:
LinkedQueue();
void Enqueue(TYPE);
void Dequeue();
TYPE queueFront();
TYPE queueRear();
bool Empty();
}; //end class LinkedQueue
24
Example - Constructor
• Constructor is created to build one empty queue.

template <class TYPE>

LinkedQueue <TYPE>::LinkedQueue()
{
front = NULL;
rear = NULL;
count = 0;
} // end Constructor

25
Example - Empty

• Empty method is to check whether the queue is


empty or not.

template <class TYPE>

bool LinkedQueue <TYPE> :: Empty()


{
return (count == 0);
} // end Empty

26
Enqueue
• Enqueue method is to push data into the rear of
queue.

• Enqueue method will cause


• front will point to new node if queue is empty.
Else, pointer rear->next will point to new node.

• The value of count is added and pointer rear


will point to a new node.
 

27
Example - Enqueue
template <class TYPE>
void LinkedQueue<TYPE>::Enqueue(TYPE value)
{ Node <TYPE> *T = new Node <TYPE>;
T->data = value;
T->next = NULL;
if (!Empty())
rear->next = T;
else
front = T;
count++;
rear = T;
rear->next = front;
} // end Enqueue
 

28
Dequeue
• Dequeue method is to remove the data from the
front of queue.

• Before Dequeue is executed make sure the queue


is empty. If the queue is empty, error message
Underflow!!! will be displayed.

• Dequeue method will cause


• pointer front and rear become NULL if the node
that is going to be removed is the only node in
the queue. If not, the pointer front will point to
the next node.
• value count decrease by one.

29
 
Example - Dequeue
template <class TYPE>
void LinkedQueue<TYPE>::Dequeue()
{ if (count == 0)// test for empty queue
cout<<“Underflow!!!!!”;
else
{ Node<TYPE> *temp = front;
if (count == 1)
front = rear = NULL;
else
front = rear = front->next;
count--;
delete temp;
}
} //end Dequeue
30
Example – Queue Front
• queueFront method is to return the data at front.

template <class TYPE>

TYPE LinkedQueue <TYPE>::queueFront()


{
if (Empty())
return false;
else
return front->data;
} // end queueFront
 

31
Example – Queue Rear
• queueRear method is to return data at the rear.

template <class TYPE>

TYPE LinkedQueue<TYPE>::queueRear()
{
if (Empty())
return false;
else
return rear->data;
} // end queueRear
 

32
Example – main function
• The code execution:

int main()
{ Queue <int> one; // create object one
one.Enqueue(40); // push 40 into one
one.Enqueue(30); //push 30 into one
one.Enqueue(20); //push 20 into one
cout << “\nFront: "<<one.queueFront();
cout << “\nRear: "<<one.queueRear();
one.Dequeue(); //remove from one
cout << “\nAfter one Dequeue:”;
cout << “\nFront: "<<one.queueFront();
return 0;
}  // end main
 

33
Example – Output
Output:

Front: 40
Rear: 20
After one Dequeue:
Front: 30
 

34
Example – Application
• Queues are the most common of all data processing
structures.
• For example online transactions such as processing
orders and customers requests.
• Queue simulation is an important application.
• In queue,a modeling activity is used to generate
statistics about the performance of queues.
• For instant, assume a counter in a bank. Assume
there is only one clerk in the counter. When there is
a customer, the customer is served if the clerk is not
busy. If there is a customer in the counter, and
another customer comes in, that customer need to
queue until his/her turn comes.  

35

You might also like