0% found this document useful (0 votes)
26 views

Lab 4

Uploaded by

Fred Neilly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lab 4

Uploaded by

Fred Neilly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures Lab: Array-based Implementation of a Queue in C++

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:

 Enqueue: Adding an element to the rear of the queue.


 Dequeue: Removing an element from the front of the queue.
 Peek/Front: Viewing the front element without removing it.
 isEmpty: Checking if the queue is empty.
 isFull: Checking if the queue is full.

Prerequisites:

 Understanding of arrays and basic data structures in C++.


 Familiarity with how a queue works (FIFO principle).

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:

1. Step 1: Define the Queue Class


o We first define the queue class, with an array and two variables front and rear
to manage the queue's front and rear positions.

#include <iostream>
using namespace std;

#define SIZE 5 // Maximum size of the queue

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;
}

// Enqueue: Adds an element to the rear of the queue


void enqueue(int value) {
if (isFull()) {
cout << "Queue Overflow! Cannot add more elements." << endl;
return;
}
rear = (rear + 1) % SIZE; // Circular increment
arr[rear] = value;
count++;
cout << "Enqueued: " << value << endl;
}

// Dequeue: Removes an element from the front of the queue


void dequeue() {
if (isEmpty()) {
cout << "Queue Underflow! No elements to remove." << endl;
return;
}
cout << "Dequeued: " << arr[front] << endl;
front = (front + 1) % SIZE; // Circular increment
count--;
}

// Peek: Returns the front element of the queue without removing it


int peek() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return -1;
}
return arr[front];
}

// isEmpty: Checks if the queue is empty


bool isEmpty() {
return count == 0;
}

// isFull: Checks if the queue is full


bool isFull() {
return count == SIZE;
}
};

2. Step 2: Demonstrating Queue Operations

2|Page
o We will now create a main() function to demonstrate the queue operations.

int main() {
Queue q; // Create a queue object

// Enqueue elements into the queue


q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);

// Attempt to enqueue into a full queue


q.enqueue(60); // This should display "Queue Overflow"

// Peek the front element


cout << "Front element is: " << q.peek() << endl;

// Dequeue elements from the queue


q.dequeue();
q.dequeue();

// Peek again after dequeue operations


cout << "Front element after dequeuing is: " << q.peek() << endl;

// Dequeue remaining elements


q.dequeue();
q.dequeue();
q.dequeue();

// Try to dequeue from an empty queue


q.dequeue(); // This should display "Queue Underflow"

return 0;
}

Step 3: Explanation of Code

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.

Step 4: Sample Output


vbnet
Copy code
Enqueued: 10
Enqueued: 20
Enqueued: 30
Enqueued: 40
Enqueued: 50
Queue Overflow! Cannot add more elements.
Front element is: 10
Dequeued: 10
Dequeued: 20
Front element after dequeuing is: 30
Dequeued: 30
Dequeued: 40
Dequeued: 50
Queue Underflow! No elements to remove.

Lab Exercise Instructions:

1. Implement the above program in your C++ environment.


2. Test the enqueue(), dequeue(), peek(), isEmpty(), and isFull() functions.
3. Explore how the queue dynamically handles elements using the circular array approach.

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:

 Enqueue: Adding an element to the rear of the queue.


 Dequeue: Removing an element from the front of the queue.
 Peek: Viewing the front element without removing it.
 isEmpty: Checking if the queue is empty.

Prerequisites:

 Understanding of linked lists and pointer manipulation in C++.


 Familiarity with the queue data structure and the FIFO principle.

Instructions:

1. Introduction to Linked List Queues: Unlike an array-based queue, a linked list-based


queue is dynamic, allowing for an unlimited number of elements until memory is full.
This implementation makes it easy to grow the queue without worrying about a fixed
size.
2. Queue Representation: In a linked list queue:
o Each node contains data and a pointer to the next node.
o The front pointer points to the first element (head) of the queue.
o The rear pointer points to the last element (tail) of the queue.

Step-by-step Implementation:

1. Step 1: Define the Node Structure


o Each element in the queue is represented as a node in a linked list. A node
contains the data and a pointer to the next node.

#include <iostream>
using namespace std;

// Node structure to represent each element in the queue


struct Node {
int data;
Node* next;

5|Page
};

2. Step 2: Define the Queue Class


o The queue class will manage the front and rear pointers, as well as the operations
on the queue.

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;
}

// Destructor to free memory when queue is destroyed


~Queue() {
while (!isEmpty()) {
dequeue();
}
}

// Enqueue: Adds an element to the rear of the queue


void enqueue(int value) {
Node* temp = new Node();
temp->data = value;
temp->next = nullptr;

if (isEmpty()) {
front = temp;
rear = temp;
} else {
rear->next = temp;
rear = temp;
}
cout << "Enqueued: " << value << endl;
}

// Dequeue: Removes an element from the front of the queue


void dequeue() {
if (isEmpty()) {
cout << "Queue Underflow! No elements to remove." << endl;
return;
}

Node* temp = front;


front = front->next;

if (front == nullptr) { // If the queue becomes empty


rear = nullptr;
}

cout << "Dequeued: " << temp->data << endl;

6|Page
delete temp; // Free the memory of the dequeued node
}

// Peek: Returns the front element of the queue without removing it


int peek() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return -1;
}
return front->data;
}

// isEmpty: Checks if the queue is empty


bool isEmpty() {
return front == nullptr;
}
};

3. Step 3: Demonstrating Queue Operations


o We will now create a main() function to demonstrate enqueue, dequeue, and peek
operations.

int main() {
Queue q; // Create a queue object

// Enqueue elements into the queue


q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

// Peek the front element


cout << "Front element is: " << q.peek() << endl;

// Dequeue elements from the queue


q.dequeue();
q.dequeue();

// Peek again after dequeue operations


cout << "Front element after dequeuing is: " << q.peek() << endl;

// Dequeue the remaining element


q.dequeue();

// Try to dequeue from an empty queue


q.dequeue(); // This should display "Queue Underflow"

return 0;
}

Step 4: Explanation of Code

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.

Step 5: Sample Output


Enqueued: 10
Enqueued: 20
Enqueued: 30
Front element is: 10
Dequeued: 10
Dequeued: 20
Front element after dequeuing is: 30
Dequeued: 30
Queue Underflow! No elements to remove.

Lab Exercise Instructions:

1. Implement the above program in your C++ environment.


2. Test the enqueue(), dequeue(), and peek() methods.
3. Experiment with different enqueue and dequeue sequences to understand how the linked
list queue behaves.

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

You might also like