Lab 4
Lab 4
Objective:
The aim of this lab is to help students understand how to implement a Queue using a static array
in C++. A queue operates on the First In, First Out (FIFO) principle, where the first element
added to the queue is the first to be removed. In this lab, students will learn about:
Prerequisites:
Instructions:
1. Introduction to Queues: A queue is a linear data structure that follows the FIFO
principle, where the first element to be added is the first to be removed. In this
implementation, a static array will be used to store elements in the queue.
2. Array-based Queue: In this implementation, the queue will be represented using an
array with fixed size. Two pointers will be used:
o front: Points to the front of the queue.
o rear: Points to the rear of the queue where new elements are added.
Step-by-step Implementation:
#include <iostream>
using namespace std;
class Queue {
private:
int arr[SIZE]; // Array to store queue elements
1|Page
int front; // Index of the front element
int rear; // Index of the rear element
int count; // Number of elements in the queue
public:
// Constructor to initialize the queue
Queue() {
front = 0;
rear = -1;
count = 0;
}
2|Page
o We will now create a main() function to demonstrate the queue operations.
int main() {
Queue q; // Create a queue object
return 0;
}
1. Queue Class:
o The array arr[SIZE] stores the queue elements.
o The front variable points to the front element of the queue.
o The rear variable points to the rear position where elements are added.
o count tracks the number of elements in the queue to check if the queue is full or
empty.
2. Circular Increment:
o The queue uses a circular array concept. When front or rear reaches the end of
the array, it wraps around to the beginning using modulo arithmetic (index + 1)
% SIZE.
3. Operations:
o Enqueue: Adds a new element to the rear, increments rear circularly, and
increases count.
3|Page
o Dequeue: Removes the front element, increments front circularly, and decreases
count.
o Peek: Returns the front element without removing it.
o isEmpty: Returns true if the queue is empty (i.e., count == 0).
o isFull: Returns true if the queue is full (i.e., count == SIZE).
4. Edge Cases:
o Trying to enqueue into a full queue results in a "Queue Overflow" message.
o Trying to dequeue from an empty queue results in a "Queue Underflow" message.
Conclusion:
In this lab, students have learned how to implement a Queue using a static array in C++. The
focus was on the core queue operations (enqueue, dequeue, peek) and handling the circular
nature of a queue using modulo arithmetic. This exercise provides a foundational understanding
of how queues work in data structures.
4|Page
Data Structures Lab: Linked List Implementation of a Queue in C++
Objective:
This lab will guide students through the process of implementing a Queue using a Linked List
in C++. A queue operates on the First In, First Out (FIFO) principle, where the first element
added is the first to be removed. The primary goal is to demonstrate the basic operations on a
queue, such as:
Prerequisites:
Instructions:
Step-by-step Implementation:
#include <iostream>
using namespace std;
5|Page
};
class Queue {
private:
Node* front; // Pointer to the front of the queue
Node* rear; // Pointer to the rear of the queue
public:
// Constructor to initialize an empty queue
Queue() {
front = nullptr;
rear = nullptr;
}
if (isEmpty()) {
front = temp;
rear = temp;
} else {
rear->next = temp;
rear = temp;
}
cout << "Enqueued: " << value << endl;
}
6|Page
delete temp; // Free the memory of the dequeued node
}
int main() {
Queue q; // Create a queue object
return 0;
}
1. Node Structure:
o A Node is a basic building block of the queue, containing data and a next
pointer that points to the next node in the queue.
7|Page
2. Queue Class:
o front: Points to the front node (the first element) of the queue.
o rear: Points to the rear node (the last element) of the queue.
o The class has the following methods:
enqueue(): Adds a new node at the rear of the queue.
dequeue(): Removes the node at the front of the queue.
peek(): Returns the front element without removing it.
isEmpty(): Returns true if the queue is empty.
3. Memory Management:
o new: Used to dynamically allocate memory for new nodes when enqueuing.
o delete: Frees the memory of dequeued nodes to avoid memory leaks.
4. Edge Cases:
o Trying to dequeue from an empty queue results in a "Queue Underflow" message.
o The queue correctly handles transitions between empty and non-empty states.
Conclusion:
In this lab, students have learned how to implement a Queue using a Linked List in C++. They
explored fundamental operations like enqueue, dequeue, and peek, along with handling edge
cases such as trying to dequeue from an empty queue. This provides a strong foundation for
understanding dynamic queue structures in data structures.
8|Page