Professional Documents
Culture Documents
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.
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.
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
0
4
Dequeue
Dequeue
1
3
40
2
front = 2
rear = 2
count= 1
2
rear = (rear + 1) % size front = 2
count = 4
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.
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.
15
Example - Empty
16
Example - Full
17
Example – Queue Front
• queueFront will return the value that is located at
the front.
18
Example – Queue Rear
• queueRear will return the value that is located at
the rear.
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.
22
Example – Node
• Definition of Node structure for queue using pointer:
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.
LinkedQueue <TYPE>::LinkedQueue()
{
front = NULL;
rear = NULL;
count = 0;
} // end Constructor
25
Example - Empty
26
Enqueue
• Enqueue method is to push data into the rear of
queue.
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.
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.
31
Example – Queue Rear
• queueRear method is to return data at the rear.
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