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

data_structure_1_fyit

Data Structure practicals

Uploaded by

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

data_structure_1_fyit

Data Structure practicals

Uploaded by

Roshan Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

FYIT- Data Structure

Prac cal Manual


Prepared by : Asst. Prof. Roshankumar Manoj Jha
Contact No : 91 7718025902
Email id : [email protected]
Data Structure Array

A : Create Array and perform opera ons like searching, sor ng and reversing the elements.

Program

#include <iostream>

using namespace std;

void displayArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

void sortArray(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

for (int j = i + 1; j < size; j++) {

if (arr[i] > arr[j]) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

void reverseArray(int arr[], int size) {

for (int i = 0; i < size / 2; i++) {

int temp = arr[i];


arr[i] = arr[size - 1 - i];

arr[size - 1 - i] = temp;

int searchElement(int arr[], int size, int key) {

for (int i = 0; i < size; i++) {

if (arr[i] == key) {

return i;

return -1;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "\nOriginal Array: ";

displayArray(arr, size);

int key;

cout << "Enter an element to search: ";

cin >> key;


int index = searchElement(arr, size, key);

if (index != -1) {

cout << "Element " << key << " found at index " << index << ".\n";

} else {

cout << "Element " << key << " not found.\n";

sortArray(arr, size);

cout << "\nSorted Array: ";

displayArray(arr, size);

reverseArray(arr, size);

cout << "\nReversed Array: ";

displayArray(arr, size);

return 0;

Output

Enter the size of the array: 5

Enter 5 elements: 1

Original Array: 1 2 3 4 5

Enter an element to search: 4

1.b Read the two arrays from the user and merge them and display the elements in sorted order.

Program

#include <iostream>
using namespace std;

void mergeArrays(int arr1[], int size1, int arr2[], int size2, int merged[], int &mergedSize) {

mergedSize = size1 + size2;

for (int i = 0; i < size1; i++) {

merged[i] = arr1[i];

for (int i = 0; i < size2; i++) {

merged[size1 + i] = arr2[i];

void sortArray(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

for (int j = i + 1; j < size; j++) {

if (arr[i] > arr[j]) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

void displayArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

}
int main() {

int size1, size2;

cout << "Enter the size of the first array: ";

cin >> size1;

int arr1[size1];

cout << "Enter " << size1 << " elements for the first array: ";

for (int i = 0; i < size1; i++) {

cin >> arr1[i];

cout << "Enter the size of the second array: ";

cin >> size2;

int arr2[size2];

cout << "Enter " << size2 << " elements for the second array: ";

for (int i = 0; i < size2; i++) {

cin >> arr2[i];

int mergedSize = size1 + size2;

int merged[mergedSize];

mergeArrays(arr1, size1, arr2, size2, merged, mergedSize);

sortArray(merged, mergedSize);

cout << "\nMerged and Sorted Array: ";

displayArray(merged, mergedSize);

return 0;

Output
Enter the size of the first array: 4

Enter 4 elements for the first array: 1

34

Enter the size of the second array: 6

Enter 6 elements for the second array: 1

Merged and Sorted Array: 1 1 2 2 3 3 4 4 5 6

1.c Write a program to perform the Matrix addi on, Mul plica on and Transpose Opera on

Program

#include <iostream>

using namespace std;

// Func on to display a matrix

void displayMatrix(int matrix[][10], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

cout << matrix[i][j] << " ";

cout << endl;

}
// Func on to add two matrices

void addMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

// Func on to mul ply two matrices

void mul plyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows1, int cols1, int
cols2) {

for (int i = 0; i < rows1; i++) {

for (int j = 0; j < cols2; j++) {

result[i][j] = 0;

for (int k = 0; k < cols1; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

// Func on to find the transpose of a matrix

void transposeMatrix(int matrix[][10], int result[][10], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[j][i] = matrix[i][j];

}
int main() {

int rows1, cols1, rows2, cols2;

// Input for Matrix 1

cout << "Enter the number of rows and columns for Matrix 1: ";

cin >> rows1 >> cols1;

int matrix1[10][10];

cout << "Enter elements of Matrix 1:" << endl;

for (int i = 0; i < rows1; i++) {

for (int j = 0; j < cols1; j++) {

cin >> matrix1[i][j];

// Input for Matrix 2

cout << "Enter the number of rows and columns for Matrix 2: ";

cin >> rows2 >> cols2;

int matrix2[10][10];

cout << "Enter elements of Matrix 2:" << endl;

for (int i = 0; i < rows2; i++) {

for (int j = 0; j < cols2; j++) {

cin >> matrix2[i][j];

// Matrix Addi on

if (rows1 == rows2 && cols1 == cols2) {

int sum[10][10];

addMatrices(matrix1, matrix2, sum, rows1, cols1);

cout << "\nMatrix Addi on Result:" << endl;

displayMatrix(sum, rows1, cols1);


} else {

cout << "\nMatrix addi on not possible (dimensions must match)." << endl;

// Matrix Mul plica on

if (cols1 == rows2) {

int product[10][10];

mul plyMatrices(matrix1, matrix2, product, rows1, cols1, cols2);

cout << "\nMatrix Mul plica on Result:" << endl;

displayMatrix(product, rows1, cols2);

} else {

cout << "\nMatrix mul plica on not possible (columns of Matrix 1 must match rows of Matrix
2)." << endl;

// Transpose of Matrix 1

int transpose1[10][10];

transposeMatrix(matrix1, transpose1, rows1, cols1);

cout << "\nTranspose of Matrix 1:" << endl;

displayMatrix(transpose1, cols1, rows1);

// Transpose of Matrix 2

int transpose2[10][10];

transposeMatrix(matrix2, transpose2, rows2, cols2);

cout << "\nTranspose of Matrix 2:" << endl;

displayMatrix(transpose2, cols2, rows2);

return 0;

2: Searching Techniques
A: Write a program to search the element using sequen al search.

Program

#include <iostream>

using namespace std;

// Func on to perform sequen al search

int sequen alSearch(int arr[], int size, int key) {

for (int i = 0; i < size; i++) {

if (arr[i] == key) {

return i; // Return the index if the element is found

return -1; // Return -1 if the element is not found

int main() {

int size, key;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "Enter the element to search: ";

cin >> key;

// Perform sequen al search


int result = sequen alSearch(arr, size, key);

if (result != -1) {

cout << "Element " << key << " found at index " << result << "." << endl;

} else {

cout << "Element " << key << " not found in the array." << endl;

return 0;

Output

Enter the size of the array: 4

Enter 4 elements: 1234

Enter the element to search: 6

Element 6 found at index 2.

2.b : Write a program to search the element using binary search

Program

#include <iostream>

using namespace std;

// Func on to perform binary search

int binarySearch(int arr[], int size, int key) {

int low = 0, high = size - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

// Check if the key is at mid


if (arr[mid] == key) {

return mid; // Element found

} else if (arr[mid] < key) {

low = mid + 1; // Search in the right half

} else {

high = mid - 1; // Search in the le half

return -1; // Element not found

int main() {

int size, key;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter " << size << " elements in sorted order: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "Enter the element to search: ";

cin >> key;

// Perform binary search

int result = binarySearch(arr, size, key);

if (result != -1) {

cout << "Element " << key << " found at index " << result << "." << endl;
} else {

cout << "Element " << key << " not found in the array." << endl;

return 0;

Output

Enter the size of the array: 4

Enter 4 elements in sorted order: 1

Enter the element to search: 5

Element 5 found at index 2.

3. Sor ng Algorithm

A: Write a program to implement Bubble Sort

Program

#include <iostream>

using namespace std;

// Func on to perform Bubble Sort

void bubbleSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

// Last i elements are already sorted

for (int j = 0; j < size - 1 - i; j++) {

if (arr[j] > arr[j + 1]) {

// Swap arr[j] and arr[j + 1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;
}

// Func on to display the array

void displayArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "\nOriginal Array: ";

displayArray(arr, size);

// Perform Bubble Sort

bubbleSort(arr, size);
cout << "Sorted Array: ";

displayArray(arr, size);

return 0;

Output

Enter the size of the array: 4

Enter 4 elements: 4321

3211

3221

2322

Original Array: 4321 3211 3221 2322

Sorted Array: 2322 3211 3221 4321

Time Complexity:

 Worst Case: O(n2)O(n^2)O(n2)

 Best Case: O(n)O(n)O(n) (if the array is already sorted).

Bubble Sort is simple but not very efficient for large datasets.

3.2: Case Study on Selec on Sort

Case Study: Selec on Sort Algorithm

Introduc on

Selec on Sort is a simple sor ng algorithm that works by repeatedly selec ng the smallest (or
largest) element from the unsorted por on of the array and moving it to its correct posi on. It is
intui ve but not very efficient compared to more advanced sor ng algorithms.

Algorithm

The Selec on Sort algorithm involves the following steps:


1. Divide the array into two parts: sorted and unsorted.

2. Iterate through the unsorted por on to find the smallest element.

3. Swap the smallest element with the first unsorted element.

4. Expand the sorted por on by one and repeat un l the array is fully sorted.

Pseudocode

less

Copy code

for i = 0 to n-1:

minIndex = i

for j = i+1 to n-1:

if arr[j] < arr[minIndex]:

minIndex = j

swap(arr[i], arr[minIndex])

Time Complexity

 Best Case: O(n2)O(n^2)O(n2)

 Worst Case: O(n2)O(n^2)O(n2)

 Average Case: O(n2)O(n^2)O(n2)

Space Complexity

 Space: O(1)O(1)O(1) (In-place algorithm).

Advantages

1. Simple to understand and implement.

2. Does not require addi onal memory.

Disadvantages

1. Inefficient for large datasets.

2. Always performs n2n^2n2 comparisons, regardless of the input.

Implementa on in C++

cpp
Copy code

#include <iostream>

using namespace std;

// Func on to perform Selec on Sort

void selec onSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

// Swap the found minimum element with the first element

int temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

// Func on to display the array

void displayArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

int main() {

int size;
cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "\nOriginal Array: ";

displayArray(arr, size);

// Perform Selec on Sort

selec onSort(arr, size);

cout << "Sorted Array: ";

displayArray(arr, size);

return 0;

Example Case

Input:

javascript

Copy code

Array: [64, 25, 12, 22, 11]

Execu on:

1. Find the smallest element (11) and swap it with the first element (64).

o Array a er 1st pass: [11, 25, 12, 22, 64]

2. Find the next smallest element (12) and swap it with the second element (25).

o Array a er 2nd pass: [11, 12, 25, 22, 64]


3. Repeat un l the array is fully sorted.

Output:

javascript

Copy code

Sorted Array: [11, 12, 22, 25, 64]

Real-World Applica ons

1. Small Datasets: Useful for sor ng small lists where simplicity is preferred over efficiency.

2. Embedded Systems: Suitable for systems with memory constraints due to its in-place
sor ng.

Conclusion

Selec on Sort is an easy-to-understand algorithm suitable for educa onal purposes. While its
O(n2)O(n^2)O(n2) me complexity makes it imprac cal for large datasets, its simplicity makes it a
good candidate for small-scale problems or as an introduc on to sor ng algorithms.

3b: Case Study on Inser on Sort

Case Study: Inser on Sort Algorithm

Introduc on

Inser on Sort is a simple sor ng algorithm that builds the final sorted array one element at a me. It
is much like sor ng playing cards in hand: you pick up one card at a me and place it in the correct
posi on rela ve to the already sorted por on. It’s efficient for small data sets or nearly sorted data
but inefficient for large data sets.

Algorithm

The basic idea behind the Inser on Sort algorithm is:

1. Start with the second element, since the first element is already considered "sorted."

2. Compare the current element with the elements in the sorted por on of the array.

3. Shi the elements of the sorted por on that are greater than the current element to the
right.

4. Insert the current element into the correct posi on in the sorted por on.

5. Repeat this process for each element un l the en re array is sorted.


Pseudocode

less

Copy code

for i = 1 to n-1:

currentElement = arr[i]

j=i-1

while j >= 0 and arr[j] > currentElement:

arr[j + 1] = arr[j]

j=j-1

arr[j + 1] = currentElement

Time Complexity

 Best Case: O(n)O(n)O(n) (when the array is already sorted)

 Worst Case: O(n2)O(n^2)O(n2) (when the array is in reverse order)

 Average Case: O(n2)O(n^2)O(n2)

Space Complexity

 Space: O(1)O(1)O(1) (In-place sor ng algorithm)

Advantages

1. Simple to implement: Inser on sort is easy to understand and implement.

2. Efficient for small datasets: For smaller datasets, inser on sort can be faster than more
complex algorithms like merge sort or quicksort.

3. Adap ve: Inser on sort is adap ve, meaning that it performs be er if the data is already
par ally sorted.

4. Stable: It does not change the rela ve order of elements with equal keys.

Disadvantages

1. Inefficient for large datasets: Its me complexity of O(n2)O(n^2)O(n2) makes it inefficient


for large datasets.

2. More comparisons and shi s: For each element, it may require many shi s and
comparisons, which can be me-consuming.
Implementa on in C++

cpp

Copy code

#include <iostream>

using namespace std;

// Func on to perform Inser on Sort

void inser onSort(int arr[], int size) {

for (int i = 1; i < size; i++) {

int currentElement = arr[i];

int j = i - 1;

// Shi elements of arr[0..i-1] that are greater than currentElement to one posi on ahead

while (j >= 0 && arr[j] > currentElement) {

arr[j + 1] = arr[j];

j = j - 1;

// Insert the current element into the correct posi on

arr[j + 1] = currentElement;

// Func on to display the array

void displayArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

}
int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "\nOriginal Array: ";

displayArray(arr, size);

// Perform Inser on Sort

inser onSort(arr, size);

cout << "Sorted Array: ";

displayArray(arr, size);

return 0;

Example Case

Input:

javascript

Copy code

Array: [64, 34, 25, 12, 22]

Execu on:

1. First Pass (i=1):


o Current element = 34

o Compare with 64: 64 > 34, so shi 64 to the right and insert 34 in the first posi on.

o Array: [34, 64, 25, 12, 22]

2. Second Pass (i=2):

o Current element = 25

o Compare with 64: 64 > 25, shi 64 to the right.

o Compare with 34: 34 > 25, shi 34 to the right and insert 25 in the first posi on.

o Array: [25, 34, 64, 12, 22]

3. Repeat the process for the remaining elements.

Output:

javascript

Copy code

Sorted Array: [12, 22, 25, 34, 64]

Real-World Applica ons

1. Small datasets: When the dataset is small, Inser on Sort can outperform more complex
algorithms.

2. Par ally sorted data: Inser on Sort works efficiently when the data is already nearly sorted.

3. Online sor ng: Inser on Sort is ideal for sor ng data in real- me or when data is arriving in a
sequen al manner.

Conclusion

Inser on Sort is a fundamental sor ng algorithm with a simple approach to sor ng elements. While
it is not suitable for large datasets due to its O(n2)O(n^2)O(n2) me complexity, it has several
advantages:

 It is very simple to implement.

 It is adap ve and performs well on par ally sorted data or small datasets.

 It is stable and requires minimal space.

For these reasons, Inser on Sort is widely used in prac ce for small data or as a part of more
complex algorithms.

4: Stack
4.a : Write a program to implement the concept of Stack with Push, Pop, Display and Exit
opera ons.

Program

#include <iostream>

using namespace std;

// Stack class defini on

class Stack {

private:

int arr[10]; // Array to hold stack elements

int top; // To track the top of the stack

public:

// Constructor to ini alize the stack

Stack() {

top = -1; // Ini ally, stack is empty

// Func on to push an element onto the stack

void push(int value) {

if (top == 9) { // Check if stack is full

cout << "Stack Overflow! Cannot push " << value << endl;

} else {

arr[++top] = value; // Increment top and add element

cout << value << " pushed onto stack." << endl;

// Func on to pop an element from the stack

void pop() {

if (top == -1) { // Check if stack is empty


cout << "Stack Underflow! Cannot pop." << endl;

} else {

cout << arr[top--] << " popped from stack." << endl; // Return the top element and decrease
top

// Func on to display the elements of the stack

void display() {

if (top == -1) {

cout << "Stack is empty." << endl;

} else {

cout << "Stack elements: ";

for (int i = 0; i <= top; i++) {

cout << arr[i] << " ";

cout << endl;

};

// Main func on to execute the stack opera ons

int main() {

Stack s; // Create a stack object

int choice, value;

while (true) {

cout << "\nStack Opera ons Menu:" << endl;

cout << "1. Push" << endl;

cout << "2. Pop" << endl;

cout << "3. Display" << endl;


cout << "4. Exit" << endl;

cout << "Enter your choice (1-4): ";

cin >> choice;

switch (choice) {

case 1: // Push opera on

cout << "Enter value to push: ";

cin >> value;

s.push(value);

break;

case 2: // Pop opera on

s.pop();

break;

case 3: // Display opera on

s.display();

break;

case 4: // Exit the program

cout << "Exi ng program." << endl;

return 0;

default:

cout << "Invalid choice! Please choose between 1-4." << endl;

return 0;

Output

Stack Opera ons Menu:

1. Push

2. Pop

3. Display
4. Exit

Enter your choice (1-4): 1

Enter value to push: 10

10 pushed onto stack.

Stack Opera ons Menu:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice (1-4): 1

Enter value to push: 20

20 pushed onto stack.

Stack Opera ons Menu:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice (1-4): 3

Stack elements: 10 20

Stack Opera ons Menu:

1. Push

2. Pop

3. Display

4. Exit

Enter your choice (1-4): 2

20 popped from stack.

Stack Opera ons Menu:


1. Push

2. Pop

3. Display

4. Exit

Enter your choice (1-4): 4

Exi ng program.

4b: Case Study on infix & pos ix opera ons

5: Queue

5.1Write a program to implement the concept of Queue with Insert, Delete, Display and Exit
opera ons.

Program

#include <iostream>

using namespace std;

// Queue class defini on

class Queue {

private:

int arr[5]; // Array to store the queue elements

int front, rear; // Front and rear indices

public:

// Constructor to ini alize the queue

Queue() {

front = -1;

rear = -1;

// Func on to insert an element into the queue (Enqueue opera on)

void insert(int value) {


if (rear == 4) { // Check if the queue is full

cout << "Queue Overflow! Cannot insert " << value << endl;

} else {

if (front == -1) {

front = 0; // If the queue is empty, set front to 0

arr[++rear] = value; // Increment rear and insert the element

cout << value << " inserted into the queue." << endl;

// Func on to delete an element from the queue (Dequeue opera on)

void deleteElement() {

if (front == -1 || front > rear) { // Check if the queue is empty

cout << "Queue Underflow! Cannot delete element." << endl;

} else {

cout << arr[front++] << " deleted from the queue." << endl; // Increment front

// Func on to display the elements of the queue

void display() {

if (front == -1 || front > rear) { // Check if the queue is empty

cout << "Queue is empty." << endl;

} else {

cout << "Queue elements: ";

for (int i = front; i <= rear; i++) {

cout << arr[i] << " ";

cout << endl;

}
}

};

// Main func on to perform opera ons on the queue

int main() {

Queue q; // Create a queue object

int choice, value;

while (true) {

cout << "\nQueue Opera ons Menu:" << endl;

cout << "1. Insert" << endl;

cout << "2. Delete" << endl;

cout << "3. Display" << endl;

cout << "4. Exit" << endl;

cout << "Enter your choice (1-4): ";

cin >> choice;

switch (choice) {

case 1: // Insert opera on

cout << "Enter value to insert: ";

cin >> value;

q.insert(value);

break;

case 2: // Delete opera on

q.deleteElement();

break;

case 3: // Display opera on

q.display();

break;

case 4: // Exit opera on

cout << "Exi ng program." << endl;


return 0;

default:

cout << "Invalid choice! Please choose between 1-4." << endl;

return 0;

Output

Queue Opera ons Menu:

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice (1-4): 1

Enter value to insert: 10

10 inserted into the queue.

Queue Opera ons Menu:

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice (1-4): 1

Enter value to insert: 20

20 inserted into the queue.

Queue Opera ons Menu:

1. Insert

2. Delete

3. Display
4. Exit

Enter your choice (1-4): 3

Queue elements: 10 20

Queue Opera ons Menu:

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice (1-4): 2

10 deleted from the queue.

Queue Opera ons Menu:

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice (1-4): 3

Queue elements: 20

Queue Opera ons Menu:

1. Insert

2. Delete

3. Display

4. Exit

Enter your choice (1-4): 4

Exi ng program.

5b: Write a program to implement the concept of Circular Queue.

Program

#include <iostream>
using namespace std;

class CircularQueue {

private:

int *arr; // Array to store queue elements

int front, rear; // Front and rear indices

int capacity; // Capacity of the queue

public:

// Constructor to ini alize the queue with a given capacity

CircularQueue(int size) {

capacity = size;

arr = new int[capacity];

front = rear = -1;

// Destructor to free the memory allocated for the queue

~CircularQueue() {

delete[] arr;

// Func on to check if the queue is full

bool isFull() {

return (rear + 1) % capacity == front;

// Func on to check if the queue is empty

bool isEmpty() {

return front == -1;

// Func on to insert an element into the queue (Enqueue)


void enqueue(int value) {

if (isFull()) {

cout << "Queue Overflow! Cannot insert " << value << endl;

} else {

if (front == -1) { // If the queue is empty, set front to 0

front = 0;

rear = (rear + 1) % capacity; // Move rear to the next posi on (circular)

arr[rear] = value; // Insert the element at the rear

cout << value << " inserted into the queue." << endl;

// Func on to delete an element from the queue (Dequeue)

void dequeue() {

if (isEmpty()) {

cout << "Queue Underflow! Cannot delete element." << endl;

} else {

cout << arr[front] << " deleted from the queue." << endl;

if (front == rear) { // If there's only one element le , reset the queue

front = rear = -1;

} else {

front = (front + 1) % capacity; // Move front to the next posi on (circular)

// Func on to display the elements of the queue

void display() {

if (isEmpty()) {

cout << "Queue is empty." << endl;


} else {

cout << "Queue elements: ";

int i = front;

while (true) {

cout << arr[i] << " ";

if (i == rear) break;

i = (i + 1) % capacity;

cout << endl;

};

// Main func on to perform opera ons on the circular queue

int main() {

int size;

cout << "Enter the size of the circular queue: ";

cin >> size;

CircularQueue q(size); // Create a circular queue of given size

int choice, value;

while (true) {

cout << "\nCircular Queue Opera ons Menu:" << endl;

cout << "1. Enqueue" << endl;

cout << "2. Dequeue" << endl;

cout << "3. Display" << endl;

cout << "4. Exit" << endl;

cout << "Enter your choice (1-4): ";

cin >> choice;


switch (choice) {

case 1: // Enqueue opera on

cout << "Enter value to enqueue: ";

cin >> value;

q.enqueue(value);

break;

case 2: // Dequeue opera on

q.dequeue();

break;

case 3: // Display opera on

q.display();

break;

case 4: // Exit opera on

cout << "Exi ng program." << endl;

return 0;

default:

cout << "Invalid choice! Please choose between 1-4." << endl;

return 0;

Output

Enter the size of the circular queue: 5

Circular Queue Opera ons Menu:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice (1-4): 1


Enter value to enqueue: 10

10 inserted into the queue.

Circular Queue Opera ons Menu:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice (1-4): 1

Enter value to enqueue: 20

20 inserted into the queue.

Circular Queue Opera ons Menu:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice (1-4): 3

Queue elements: 10 20

Circular Queue Opera ons Menu:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice (1-4): 2

10 deleted from the queue.

Circular Queue Opera ons Menu:

1. Enqueue

2. Dequeue
3. Display

4. Exit

Enter your choice (1-4): 3

Queue elements: 20

Circular Queue Opera ons Menu:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice (1-4): 4

Exi ng program.

_______________________________***End***_________________________________________

You might also like