Professional Documents
Culture Documents
Objectives: To Understand Queue Operation in An Linear List (Array) - To Understand Queue Operation in A Linked List
Objectives: To Understand Queue Operation in An Linear List (Array) - To Understand Queue Operation in A Linked List
DITP 2113
2113 Data
Data Structure
Structure and
and Algorithm
Algorithm
Lecture
Lecture 99 :: Queue
Queue (Part
(Part 1)
1)
Objectives:
To understand queue operation in an linear
list (array).
To understand queue operation in a linked
list.
Introduction
In our daily life, we see queue as a waiting
line.
This queue can be seen at counters for
instance post office, bank, and plaza toll.
In normal practice, when someone is
queuing the first person is served (first
come first serve) and continues for the
next person and so on, unless for over
queue.
2
Concept
• Queue is defined as a list that we can add an item at the end (rear)
and remove an item at the other end (front).
• The general characteristics of a queue is that the item that goes in
first will be removed first. (First In First Out-FIFO)
• The diagram below shows a queue:
A B C D E
out in
front rear
3
Concept(Cont…)
• Queue is a data structure that is suitable for items that need to be
processed in sequence.
• Queue is usually used for modeling the queue in computer system
like queuing files to be sent to the printer for several computers
that share one printer.
• The basic operation for queue are:
1.Insert data at the rear (Enqueue).
2.Delete data at the front (Dequeue).
3.Retrieve data from the front (Queue Front).
4.Retrieve data from the rear (Queue Rear).
4
Basic Operations
1. ENQUEUE
5
Basic Operations (Cont…)
2.DEQUEUE
Deletion in queue is known as dequeue. The data at
the front of the queue is returned to the user and
removed from the queue. If the queue is empty, and
the delete process is attempt, the queue will be in
underflow state.
6
Basic Operations (Cont…)
3. QUEUE FRONT
4. QUEUE REAR
7
Implementation Method
Queue Implementation Method:
8
Implementation Method(Cont…)
• The value for variable rear for last location in the queue will be added
when an item is added to the queue.
• The value for variable front will be added when an item is removed
from the queue.
• Assume an array with 5 elements. The sequence for the operations is
as below:
Enqueue 30
Enqueue 70
Enqueue 40
[0] [1] [2] [3] [4]
30 70 40
Front = 0 Count=3
Rear=2
9
Implementation Method (Cont…)
• Let say two items is being removed from the queue. The modified
queue is as below :
40
Count = 1
Front = 2 Rear = 2
10
Implementation Method (Cont…)
After that the operations below is executed:
Enqueue 50
Enqueue 80
Front = 2 Rear=4
11
Implementation Method (Cont…)
• See that the elements in array referred, exceed the maximum array index that has
been defined.
• And the number of items in the queue is 3 and there are left 2 empty places in the
array.
• If we want to add an item and maintain the characteristics of the queue, we need to
move the element in the queue so that the first item will be located at index 0,
second item at index 1 and third item at index 2. The value at the front and the rear
need to be updated. As the diagram below :
Front = 0 Rear= 2 12
Implementation Method
13
Implementation Method Using Array
Implementation of Queue using the Array in C++.
14
Implementation Method Using Array
Physical diagram showing queue using the
array.
12 100 45 6
[0] [1] [2] [3] [4]
15
Example – Class Definition
const int size = 5;
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();
16
}; //end class Queue
Example - Constructor
• Constructor is created to make sure that before
Queue<TYPE>::Queue()
{
front = -1;
rear = -1;
count = 0;
} // end Constructor
17
Enqueue
• Enqueue method is used to enqueue data at rear
of the queue.
18
Example - Enqueue
template <class TYPE>
void Queue<TYPE>::Enqueue(TYPE value)
{ if (!Empty()) { // test if queue is not empty
if (!Full()){ // test if not full
rear++;
arr[rear] = value;
count++;
}
else
cout<<“Overflow!!!”;
}
else { // if queue is empty
rear++;
front++;
arr[rear] = value;
count++;
}
} // end Enqueue
19
Dequeue
• Dequeue method is used to delete or remove data
from the front queue.
20
Example - Dequeue
template <class TYPE>
void Queue<TYPE>::Dequeue()
{
if (!Empty())//test if queue is not empty
{
if (front == size – 1) //test if last value
{ front = rear = -1;
count--; }
else {
front++;
count--; }
}
else // if queue is empty
{
cout<<“Underflow!!!!!”;
}
} //end Dequeue
21
Example - Empty
22
Example - Full
23
Example – Queue Front
• queueFront will return the value that is located at
the front.
24
Example – Queue Rear
• queueRear will return the value that is located at
the rear.
25
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
26
Example – Output
Output:
Front: 40
Rear: 20
After one Dequeue:
Front: 30
27
Implementation Method Using Linked List
28
Implementation Method Using Linked List
29
Implementation Method Using Linked List
30
Example – Node
• Definition of Node structure for queue using pointer:
struct Node
{ TYPE data;
Node <TYPE> *next;
};
31
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
32
Example - Constructor
• Constructor is created to build one empty queue.
LinkedQueue <TYPE>::LinkedQueue()
{
front = NULL;
rear = NULL;
count = 0;
} // end Constructor
33
Example - Empty
34
Enqueue
• Enqueue method is to push data into the rear of
queue.
35
Example - Enqueue
template <class TYPE>
void LinkedQueue<TYPE>::Enqueue(TYPE value)
{ Node <TYPE> *newNode = new Node <TYPE>;
newNode->data = value;
newNode->next = NULL;
if (!Empty()) // test if queue is not
empty
rear->next = newNode;
else
front = newNode;
count++;
rear = newNode;
} // end Enqueue
36
Dequeue
• Dequeue method is to remove the data from the
front of queue.
37
Example - Dequeue
template <class TYPE>
void LinkedQueue<TYPE>::Dequeue()
{ if (Empty())// test if queue is empty
cout<<“Underflow!!!!!”;
else
{ Node<TYPE> *temp = front;
if (count == 1) // test if last node
rear = front = NULL;
else
front = front->next;
count--;
delete temp;
}
} //end Dequeue
38
Example – Queue Front
• queueFront method is to return the data at front.
39
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
40
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
41
Example – Output
Output:
Front: 40
Rear: 20
After one Dequeue:
Front: 30
42