You are on page 1of 42

DITP

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

Inserting a queue is known as enqueue. After the data is


inserted into the queue, the new element becomes the rear.
If there is not enough room for another element in the
queue, there will be overflow.

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

Data at the front queue can be retrieved using queue front.


It returns the front data without changing the contents of the
queue.

4. QUEUE REAR

A parallel operation to queue front to retrieve data at the


rear of the queue. It is known as queue rear.

7
Implementation Method
Queue Implementation Method:

1. Implementation of queue as an array


2. Implementation of queue as a linked list (using pointer)

Implementation of queue as an Array


• The definition is similar with stack’s data structures that use array.
• Queue use one array to store the queue items and three variables
to represent the location for front, rear and count in the queue.

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 :

[0] [1] [2] [3] [4]

40

Count = 1

Front = 2 Rear = 2

10
Implementation Method (Cont…)
After that the operations below is executed:
Enqueue 50
Enqueue 80

The effect to the queue is as below:

[0] [1] [2] [3] [4]


Count = 3
40 50 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 :

[0] [1] [2] [3] [4]


Count = 3
40 50 80

Front = 0 Rear= 2 12
Implementation Method

How to overcome those problems?

Using Circular Queue (will be


covered in Queue Part 2).

13
Implementation Method Using Array
Implementation of Queue using the Array in C++.

• Apart from array as the data member, queue also


need to create three more data members which are
the count, front and rear.

• The frequent methods used in queue are


constructor, enqueue, dequeue, queue front,
queue rear and empty.

14
Implementation Method Using Array
Physical diagram showing queue using the
array.

count front rear


Queue arr
4 0 3

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

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
17
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
• front value is added only when the queue is
empty

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.

• 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
• if there is 1 item in the queue, after dequeue
front and rear value will be -1
• count value will decrease with one

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

• 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

 
22
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 (rear == size - 1);
} //end Full

 
23
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
 

24
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
 

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

Implementation of queue as a linked list (using pointer)

• Queue using array is more easy and efficient


because the number of elements is fix.

• Problem arises if the queue size is not known. This


will cause queue overflow.

• So, the linked queue is more suitable as long as the


computer memory is not full.

28
Implementation Method Using Linked List

The physical diagram showing queue using the


pointer

front count rear


N

data next data next data next data next

29
Implementation Method Using Linked List

Queue Implementation Using Pointer in C++.

• Normally, besides attribute, queue use the pointer


that is controlled by 2 pointers which are front and
rear.

• Pointer front is to point to first nod in the list while


pointer rear is to point to the last nod in the list.

• The common methods are constructor, enqueue,


dequeue, queue front, queue rear and empty.
 

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

template <class TYPE>

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.

template <class TYPE>

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

33
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

34
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.
 

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.

• 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.

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.

template <class TYPE>

TYPE LinkedQueue <TYPE>::queueFront()


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

39
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
 

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

You might also like