DS Lab 6 - Queue Implementation
DS Lab 6 - Queue Implementation
Table of Contents
1. Introduction............................................................................................................................................................3
1.1 Queue Operations..................................................................................................................................................3
1.3Relevant Lecture Readings..........................................................................................................................................3
2. Activity Time boxing.............................................................................................................................................3
3. Objectives of the experiment.................................................................................................................................3
4. Concept Map..........................................................................................................................................................4
4.1 Queue as a static array.................................................................................................................................................4
5. Homework before Lab.........................................................................................................................................................6
5.1 Problem Solution Modeling........................................................................................................................................6
5.2 Practices from home....................................................................................................................................................6
5.3.1 Task-2......................................................................................................................................................................6
6. Procedure& Tools................................................................................................................................................................6
6.1 Tools............................................................................................................................................................................6
6.2 Walk through Tasks [Expected time = 20mins].................................................................................................6
7. Practice Tasks......................................................................................................................................................................8
7.1Practice Task 1 [Expected time = 20mins]..........................................................................................................8
7.2 Practice Task 2 [Expected time = 20 mins]................................................................................................9
7.3 Practice Task 3 [Expected time = 20 mins]................................................................................................9
7.4 Out comes....................................................................................................................................................................9
7.5Testing........................................................................................................................................................................10
8.Evaluation Task (Unseen) [Expected time = 60mins for two tasks].............................................................................11
9. Evaluation criteria.............................................................................................................................................................11
10. Further Readings.............................................................................................................................................................12
10.1 Web sites related to C++ tutorials related to queues...............................................................................................12
10.2 Web sites containing supporting material...............................................................................................................12
1. Introduction
This lab will help students to learn about using the stacks as postfix calculator and infix to postfix
convertor. Also this lab will introduce use of FIFO (First In First Out) Queue and operations which may
perform on this queue. A queue is a specific type of abstract data type in which the elements are kept in
order and the only operations on the collection are the addition of elements to the rear terminal position,
which is called addQueue or enqueue, and removals of elements from the front terminal position, known
as removeQueue or dequeue.In a FIFO data structure, the first element which is added to the queue will be
the first element to be removed from queue.
Two key operations which are defined for queues are addQueue (enqueue) and deleteQueue
(dequeue). As elements cannot be deleted from an empty queue and cannot be added to a full queue,
therefore we need two more operations called IsEmptyQueue (to check whether a queue is empty) and
IsFullQueue (to check whether a queue is full). Another operation which is required for queues is
initializeQueue: to initialize a queue in empty state, it means it should not contain any elements in it when
queue is created. To retrieve the front and last elements of queue we need to have two operations called
front and last operations.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in lab.
Before writing the algorithms for queue operations, we need to decide that how queueFront and
queueRear will be used to access the queue elements. How queueFront and queueRear indicate that status
of queue that the queue is empty or full? If queueFront gives the index of the first element of the queue
and queueRear gives the index of the last element of the queue. To add an element to the queue, first we
advance queueRear to the next array position and then add the element to the position that queueRear is
pointing to. To delete an element from the queue, first we retrieve the element that queueFront is pointing
to and then advance queueFront to the next element of the queue. It means queueFront will be changing
after each deleteQueue operation and queueRear will be changing after each addQueue operation.
Let us see what will happen when queueFront changes after a deleteQueue operation and
queueRear changes after an addQueue operation. Assume that size of array to holding queue elements is
100.
After this operation, the array containing the queue is as shown in Figure 3.
An array for a queue is normally initialized by setting queueFront and queueRear to -1.
queueRear=-1; queueFront=-1;
When an element is required to be added in queue, we need to check that whether queue overflow
condition occurs or not, it means whether queue will exceed its maximum size on addition of an element
to queue or not? If x is an element to be inserted in a queue based on array “Queue” with a maximum size
MAX_QUEUE, then following code segment represents implementation of addQueue or enqueue
operation.
if(queueRear> MAX_QUEUE-1)
{ cout<<"queue over flow";
queueFront=queueRear=-1;
return; }
Queue[++queueRear]=x;
cout<<"inserted" <<x;
When an element is required to be remove from a queue, we need to check that whether queue is empty or
not, because we cannot remove an element from an empty queue. Following code segment represents this
operation.
if(queueFront==queueRear)
{ cout<<"queue under flow"; return; }
cout<<"deleted" <<Queue[++queueFront];
solution of following problems. Design the solutions of the problems in C++ and bring your code
to lab so that lab instructor should assess and grade it.
5.2 Practices from home
5.3.1 Task-2
Compare implementations of stack with a queue using static array and provide at least three
differences between these two implementations.
6. Procedure& Tools
This section provides information about tools and programming procedures used for the lab.
6.1 Tools
Microsoft Visual Studio 2017 with Visual C++ compiler configured.
6.2 Walk through Tasks [Expected time = 20mins]
In this task we will implement and test the evaluation of post fix expression and a program for a queue
using static array using C++.Figure 4 and 5 represents the source code view of queue program in visual
studio. You are required to type this code and further debug and execute the program.
Following is output window of this program in figure 4 and figure 5, when executed.
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 place solution code in a folder specified
by your lab instructor.
Partial solution of a Linear Queue Class is given below. Complete the missing code in the functions to run the
program properly.
Class Queue
{
int *elements;
int size;
int rear, front;
////////////////////////////////// Constructor
Queue (int maxsize)
{
size=maxsize;
elements=new int[size];
rear = 0, front = -1;
}
/////////////////////////////////// Destructor
~ Queue()
{
delete []elements;
}
/////////////////////////////////// Output the Queue structure
void showStructure ()
{
if(!isEmpty())
{
for(int i=0;i<rear;i++)
{
cout<<"Element:"<<elements[i]<<endl;
}
}
else
{
cout<<"Display: No items to be displayed. Queue is empty\n";
}
}
////////////////////////////////// Queue manipulation operations
// Insert in queue
void Enqueue (int newDataItem)
{
if(!isFull())
{
if(front==-1)
{front=0;}
elements[rear]=newDataItem;
rear++;
}
else
{
cout<<"Insert: Cannot insert more items. List is full\n";
}
}
// Remove data item
void Dequeue ()
{
if(!isEmpty())
{
//implement your logic here
// delete a data item from front end and shift all the remaining items to fill the gap at the front.
}
else
{
cout<<"Remove: Cannot remove the item. List is empty\n";
}
}
// Clear queue
void clear ()
{
Department of Computer Science Page 8
IIU, Islamabad
Lab 6: Queue Implementation in C++
Write a program to implement Parking area reservation. The parking area has only one way out and one-
way inn; so scheduling has to be done for the arrival and departure of vehicles in the parking area. At one
time, only one vehicle can arrive or leave the area. Scheduling has to be done on the basis of first come,
first serve. Whichever vehicle comes first at the parking area will be given the space in the parking area.
Similarly, whichever vehicle requests for departure first, will be given the space to the exit.
[Hint: Arrival of vehicle=enqueue, Departure of vehicle=dequeue]
7.3 Practice Task 3 [Expected time = 20
mins]
A queue can be used to simulate the flow of customers through a check-out line in a departmental store.
Your program must store Name, Customer_id and Bill of each customer. You can model the flow of
customers using a queue in which each data item corresponds to a customer in the line. Customers come
one by one and leave the queue after check-out. Each customer takes approx. 5 minutes to check out.
Implement the given system and provide the following functionality to user:
1) Add a customer to queue
2) Delete a customer from queue after check out
3) Calculate the total number of customers served
4) Calculate the time required to serve all the remaining customers
7.5Testing
Test Cases for Practice Task-1
Sample Sample
Inputs-1 Outputs-1
Enter values to be inserted for queue
2
3
4
7
8
Dequeue: 2
Dequeue: 3
Dequeue: 4
Values in queue: 7 8
Sample Sample
Inputs-1 Outputs-1
Insert elements in queue:
Car ID: SH435
Car ID: PI567
Car ID: AB984
Element removed from rear of queue is
Car ID: SH435
Test Cases for Practice Task-3
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).