Computer Science Department
[0921-211]: [Data Structure and Algorithms]
Section: [Male/Female]
Lab [06]: [Queue: representation and functions
(Linear and Circular)]
Date: []
]Data Structure and Algorithms [ :]0921-211[ 1|Page
Introduction
Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The
order is First In First Out (FIFO). A good example of queue is any queue of consumers for a resource where the
consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we
remove the item the most recently added; in a queue, we remove the item the least recently added.
Queue Operations
Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the same order in which they are
pushed. If the queue is empty, then it is said to be an Underflow condition.
Front: Get the front item from queue.
Rear: Get the last item from queue.
Display: Print all element of the queue. If the queue is non-empty, traverse and print all the elements from
index front to rear.
Linear Queue
A queue is an ordered list in which items may be added only at one end called the “rear” and items may be removed
only at the other end called “front”.
Circular Queue
A circular queue is an extension of the basic queue which is also known as “Ring
buffer”. It is a linear data structure that is used to store data items. It performs
operations by following the FIFO (First In, First Out) approach and the last
position in the queue is connected back to the first position to form a circle.
Objectives
:At the end of this lab session the students should be able to
1- Differentiate linear and circular queue.
2- Implement queue operations.
3- Solve programming problem using queue.
Tools/Software Requirement
- Dev C++ IDE
Description
This lab manual contain details on how to implement Queue (Liner and Circular) in C++. The lab manual is divided
into 2 parts:
]Data Structure and Algorithms [ :]0921-211[ 2|Page
Part 1: Examples
- Students should go through the given examples (Queue: representation and functions (linear and circular)).
- Use Dev C++ IDE to run the code.
Part 2: Lab Tasks
- Students should perform the lab experiments and answer questions based on these experiments.
- Programming Exercise: Student should write a code to implement Queue (linear and circular).
Deliverables:
Students should submit the following:
1. PDF file that contains:
a. Answer for question 1 to 5?
b. Source code and screen shot for the programming exercise.
Part 1: Examples
1.1 Linear Queue
To implement a queue, create an array arr of size n and take two variables front and rear both of which will be
initialized to 0 which means the queue is currently empty. Element rear is the index up to which the elements are
stored in the array and front is the index of the first element of the array.
A. Declaring Queue Structure
#include <iostream>
using namespace std;
#define MAX 5 //defines array size
struct Queue { //define Queue structure
int front, rear;
int queue[MAX];
};
Queue q; //declare structure variable
B. Enqueue Function
// function to insert an element at the rear of the queue
void enqueue(int data)
{
// check queue is full or not
if (MAX == q.rear)
{
cout << "\nQueue is full\n";
return;
}
]Data Structure and Algorithms [ :]0921-211[ 3|Page
// insert element at the rear
else {
q.queue[q.rear] = data;
q.rear++; //increment rear value
}
}
C. Dequeue Function
// function to delete an element from the front of the queue
void dequeue()
{
// if queue is empty
if (q.front == q.rear) {
cout << "\nQueue is empty\n";
return;
}
// remove front element.
else {
cout << q.queue[q.front] << "<-- data removed!\n";
q.queue[q.front] = 0;
q.front++; // increment front
}
}
D. Display Queue Elements
// print all queue elements
void display()
{
int i;
if (q.front == q.rear)
{ //check queue if empty
cout << "\nQueue is Empty\n";
return;
}
// traverse front to rear and print elements
for (i = q.front; i < q.rear; i++)
{
cout << q.queue[i] << " <-- ";
}
}
]Data Structure and Algorithms [ :]0921-211[ 4|Page
E. Front Function
// print front of queue
void front()
{
if (q.front == q.rear) //check if queue is empty
cout << "\nQueue is Empty\n";
//diplay front queue
else
cout << "\nFront Element is: " << q.queue[q.front];
}
F. Rear Function
// print rear of queue
void rear()
{
if (q.front == q.rear) //check if queue is empty
cout << "\nQueue is Empty\n";
//display rear queue
else
cout << "\nRear Element is: " << q.queue[q.rear-1];
}
G. Driver Code
int main()
{
q.front = q.rear = 0; //Initialize front and rear to 0.
int choice, data;
do
{
//display Queue operations on a menu
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Front\n";
cout << "4. Rear\n";
cout << "5. Display Queue\n";
cout << "6. Exit\n";
cout << "\nEnter your choice: ";
cin >> choice;
]Data Structure and Algorithms [ :]0921-211[ 5|Page
switch(choice)
{
case 1: // Call Enqueue Function
cout << "\nEnter data to enqueue: ";
cin >> data;
enqueue(data);
break;
case 2: // Call Dequeue Function
dequeue();
cout << "\n";
break;
case 3: // Call Front Function
front();
cout << "\n";
break;
case 4: // Call Rear Function
rear();
cout << "\n";
break;
case 5: // Call Display Function
display();
cout << "\n";
break;
case 6: // Terminates program
cout << "\nProgram terminated..";
break;
default: // Incorrect Input message
cout << "\nIncorrect Input\n";
}
}while(choice!=6);
return 0;
}
]Data Structure and Algorithms [ :]0921-211[ 6|Page
H. Program Execution Screenshot
Display Queue Enqueue Function
Front Function Dequeue Function
1.2 Circular Queue
In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert
the next element even if there is a space in front of queue. In the circular queue, when the queue is full, and when we
remove elements from the front since last and first positions are connected, we can insert the elements at the rear
which was vacated by deleting the element. Note: Initialize front and rear to -1.
Note: Code below are changes from the Linear Queue Implementation (Enqueue, Dequeue and Display function).
A. Declaring Queue Structure
#include <iostream>
using namespace std;
#define MAX 5 //defines array size
struct Queue { //define Queue structure
int front, rear;
int queue[MAX];
};
Queue q; //declare structure variable
]Data Structure and Algorithms [ :]0921-211[ 7|Page
B. Enqueue Function
// Function for Enqueue
void enqueue(int value)
{
//check if Queue in empty
if ((q.front == 0 && q.rear == MAX-1) || (q.rear == q.front-1))
{
cout << "\nQueue is Full\n";
}
else if (q.front == -1) // Insert First Element
{
q.front = q.rear = 0;
q.queue[q.rear] = value;
}
else if (q.rear == MAX-1 && q.front != 0)
{
q.rear = 0;
q.queue[q.rear] = value;
}
else
{
q.rear++;
q.queue[q.rear] = value;
}
}
C. Dequeue Function
// Function to delete element from Circular Queue
void dequeue()
{
if (q.front == -1) // check if queue is empty
{
cout << "\nQueue is Empty\n";
return;
}
int data = q.queue[q.front];
q.queue[q.front] = -1;
if (q.front == q.rear) // removing last element
{
]Data Structure and Algorithms [ :]0921-211[ 8|Page
q.front = -1;
q.rear = -1;
}
else if (q.front == MAX-1)
q.front = 0;
else
q.front++;
cout << "\nRemoved Data: " << data;
}
D. Display Queue Elements
// Function displaying the elements of Circular Queue
void display()
{
if (q.front == -1)
{
cout << "\nQueue is Empty\n"; //check if queue is empty
return;
}
//traverse through the array to display queue elements
cout << "\nElements in Circular Queue are: ";
if (q.rear >= q.front)
{
for (int i = q.front; i <= q.rear; i++)
cout << q.queue[i] << " ";
}
else
{
for (int i = q.front; i < MAX; i++)
cout << q.queue[i] << " ";
for (int i = 0; i <= q.rear; i++)
cout << q.queue[i] << " ";
}
}
E. Front Function
// print front of queue
void front()
{
if (q.front == -1) //check if queue is empty
cout << "\nQueue is Empty\n";
//diplay front queue
else
cout << "\nFront Element is: " << q.queue[q.front];
]Data Structure and Algorithms [ :]0921-211[ 9|Page
}
F. Rear Function
// print rear of queue
void rear()
{
if (q.front == -1) //check if queue is empty
cout << "\nQueue is Empty\n";
//display rear queue
else
cout << "\nRear Element is: " << q.queue[q.rear];
}
G. Driver Code
int main()
{
q.front = q.rear = -1; //Initialize front and rear to 0.
int choice, data;
do
{
//display Queue operations on a menu
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Front\n";
cout << "4. Rear\n";
cout << "5. Display Queue\n";
cout << "6. Exit\n";
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice)
{
case 1: // Call Enqueue Function
cout << "\nEnter data to enqueue: ";
cin >> data;
enqueue(data);
break;
case 2: // Call Dequeue Function
dequeue();
]Data Structure and Algorithms [ :]0921-211[ 10 | P a g e
cout << "\n";
break;
case 3: // Call Front Function
front();
cout << "\n";
break;
case 4: // Call Rear Function
rear();
cout << "\n";
break;
case 5: // Call Display Function
display();
cout << "\n";
break;
case 6: // Terminates program
cout << "\nProgram terminated..";
break;
default: // Incorrect Input message
cout << "\nIncorrect Input\n";
}
}while(choice!=6);
return 0;
}
H. Program Execution Screenshot
Display Queue Enqueue Function
Front Function Dequeue Function
]Data Structure and Algorithms [ :]0921-211[ 11 | P a g e
Part 2: Lab Tasks: (10 points)
Problem: Reversing a Queue
Description
Write a program that will reverse the content of a Queue using the following standard queue operations.
enqueue(x) : Add an item x to rear of queue.
dequeue() : Remove an item from front of queue.
empty() : Checks if a queue is empty or not.
For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a
manner such that if we re-insert the elements in the queue they would get inserted in reverse order. So now our task
is to choose such data-structure which can serve the purpose. According to the approach, the data-structure should
have the property of ‘LIFO’ as the last element to be inserted in the data structure should be the first element of the
reversed queue. Using the new data structure to store the elements of the queue temporarily should:
Pop the elements from the queue and insert into the new data structure. (first element of the data structure is
last element of the queue)
Pop the elements of the data structure to insert back into the queue. (The last element of the data structure is
the first one to be inserted into the queue)
Input Format
The program should accept N series of integer values where 1 < N < 10 separated by a space and stored in a queue.
If the input N integer exceeds the allowed number of input the program should display an error message.
Example Input
Enter N Integer values: 1 2 3 4 5 6 7 8 9 10
]Data Structure and Algorithms [ :]0921-211[ 12 | P a g e
Output Format
The program should display the reverser order of the queue elements in a single line separated by a space. (Note:
that only queue standards operations can be used to display the queue elements).
Example Output
Reverse Queue: 10 9 8 7 6 5 4 3 2 1
Example Program Execution
Enter N Integer values: 10 40 60 20 80
Reverse Queue: 80 20 60 40 10
]Data Structure and Algorithms [ :]0921-211[ 13 | P a g e