DS Lab 7 - Circular and Priority Queues
DS Lab 7 - Circular and Priority Queues
Table of Contents
1. Introduction............................................................................................................................................................3
1.1 Circular Queues.....................................................................................................................................................3
1.2 Priority Queues......................................................................................................................................................3
1.3Relevant Lecture Readings..........................................................................................................................................3
2. Activity Time boxing.............................................................................................................................................4
3. Objectives of the experiment.................................................................................................................................4
4. Concept Map..........................................................................................................................................................4
4.1 Circular Queues in C++..............................................................................................................................................4
4.2Priority Queues in C++................................................................................................................................................5
5. Homework before Lab.........................................................................................................................................................7
5.1 Problem Solution Modeling........................................................................................................................................7
5.2Problem description:....................................................................................................................................................7
5.3Practices from home.....................................................................................................................................................8
5.3.1 Task-1......................................................................................................................................................................8
5.3.2 Task-2......................................................................................................................................................................8
6. Procedure& Tools................................................................................................................................................................8
6.1 Tools............................................................................................................................................................................8
6.2 Walk through Tasks [Expected time = 20mins]..............................................................................................8
7. Practice Tasks....................................................................................................................................................................10
7.1 Practice Task 1 [Expected time = 20 mins]..................................................................................................10
7.2 Practice Task 2 [Expected time = 30 mins]..................................................................................................11
7.4Out comes...................................................................................................................................................................11
7.6Testing........................................................................................................................................................................11
8. Evaluation Task (Unseen) [Expected time = 60mins for two tasks].......................................................................13
9. Evaluation criteria.............................................................................................................................................................13
10. Further Readings.............................................................................................................................................................14
10.1 Web sites related to C++ tutorials related to circular and priority queues..............................................................14
10.2 Web sites containing supporting material...............................................................................................................14
1. Introduction
This lab will introduce concept of circular and priority queues. In standard queues when dequeue
operation is performed a problem called de-bufferring occurs, to solve this problem we need to use
circular queues. Beside the circular queues we may also use priority queues, in such queues we may delete
an element from queue on the basis of some priority. In First in first out queues, the element at start of
queue has highest priority to be removed from queue.
Circular queuesare linear data structures. It follows FIFO (First In First Out) rule.
In circular queue the last element is connected back to the first element to make a circle.
Elements are inserted at the rear end and the elements are removed from front end of the
queue.
Both the front and the rear pointers point to the beginning of the array.
It is also called as “Ring buffer”.
Priority queues are used in such situations where you want to remove an element from queue
which is not at the moment at front of queue, but its priority is high. For example in daily life if in some
hospital there is a queue of patients waiting for checkup/treatment from doctor. If some patient comes
whose have light threatening symptoms and need urgent treatment then he should be at high priority in
that queue and should be removed from queue on his priority, may be patient at front of queue at the
moment has come for general checkup.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in lab.
4.1 Circular Queues in C++
Suppose we have given an array to implement a FIFO queue. We are using queueFront and
queueRear two elements referring indices of this array. Now if we perform addition and removal of
element on this array in such a way that after addition of three elements we remove one element. On
performing addition queueRear will move to index of next element of array. When queueRear will point
to last element of array after that it cannot use any other element of array for insertion of further element
in this array. Figure 1 shows this scenario.
So we may use this array in circular fashion so that rear can move to starting element after
reaching last element of array. Figure 2 represents the logical circular array which can be used.
Because the array containing the queue is circular, we can use the following statement to
advance queueRear (queueFront) to the next array position:
If queueRear<maxQueueSize - 1,
ThenqueueRear + 1 <= maxQueueSize - 1,
so(queueRear + 1) % maxQueueSize = queueRear + 1.
IfqueueRear == maxQueueSize- 1
queueRear + 1 ==maxQueueSize,
struct data
{
char job[MAX];
int prno ;
int ord ;
} ;
struct pque
{
data d[MAX];
int front ;
int rear ;
} ;
pq -> rear++ ;
pq -> d[pq -> rear] = dt ;
if ( pq -> front == -1 )
pq -> front = 0 ;
if ( pq -> front == -1 )
{
cout<<"\nQueue is Empty."<<endl;
return t ;
}
return t ;
}
7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed in lab
session. Design solutions of problems discussed in each task and placesolution code in a folder specified
by your lab instructor.
Lab instructors are guided to help students learn how ACM problems work and provide students
with certain practice/home tasks on the pattern of ACM Problems.
basis of Experience (must be more than 3 years), Qualification (must have done MS) and
Written_test_marks (More marks high priority). Whenever panel calls a candidate the person whose
priority is highest must give the interview. Your program should display all the candidates in the priority
queue as well.
7.6Testing
Test Cases for Practice Task-1
Sample Input Sample Output
Insert elements in rear of circular
queue:
2
3
4
5
7
8
6
9
11
1
Enter number of elements to be 2
removed from circular queue: 4
3
4
5
Average of removed values is: 3.5
Enter choice: 1
Enter your Name: Firdous
Enter your CNIC: XXXXX-XXXXXXX-X
Enter your Age: 35
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student has
finished the complete/partial task(s).