0% found this document useful (0 votes)
4 views10 pages

Tanguin2b Circular Machprobdsa

The document outlines a machine problem for a Data Structures and Algorithm course, focusing on the implementation of a Circular Queue in C++. It details the operations of enqueueing, dequeueing, displaying elements, and exiting the program, emphasizing the importance of handling edge cases and providing user feedback. The project aims to enhance understanding of circular queues and their applications in efficient data management.

Uploaded by

lancetanguin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Tanguin2b Circular Machprobdsa

The document outlines a machine problem for a Data Structures and Algorithm course, focusing on the implementation of a Circular Queue in C++. It details the operations of enqueueing, dequeueing, displaying elements, and exiting the program, emphasizing the importance of handling edge cases and providing user feedback. The project aims to enhance understanding of circular queues and their applications in efficient data management.

Uploaded by

lancetanguin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Bicol University

Polangui

Data Structures and


Algorithm
Machine Problem:
(Circular)

By: Lancelot R. Tanguin


Course & Block: BSCpE 2B
Professor: Melissa Jeankie Satiada-Rellon

Statement Problem
In modern data management and computer science, queues are essential
data structures for efficiently handling ordered data. A Circular Queue, in
particular, is a variant of the linear queue that optimizes space utilization by
reusing empty slots once data is dequeued. This makes it especially useful in
scenarios like resource scheduling, memory management, and buffering. The
challenge lies in implementing a user-based program to demonstrate the
operations of a circular queue effectively.

Imagine a scenario where a task scheduler processes jobs in a circular


fashion, ensuring all tasks are managed efficiently within a fixed buffer. Users
must interact with the system to enqueue tasks, remove tasks once
completed, and view the current status of the queue. This ensures that no
memory space is wasted, as empty slots left by completed tasks can be
reused for new entries.

Design a program in C++ that allows users to perform the following operations on a circular
queue:

1. Enqueue: Add an element to the queue if there is space available.


Handle scenarios where the queue is full and provide appropriate
feedback to the user.
2. Dequeue: Remove the oldest element from the queue, ensuring the
front pointer adjusts correctly for circular behavior. Handle cases where
the queue is empty.
3.  Display: Display all the elements in the queue in the correct order,
whether the data is stored sequentially or wrapped around the queue’s
end.
4.  Exit: Allow the user to terminate the program at any time.

The program should handle edge cases such as full and empty queues while
using modular arithmetic to ensure the circular behavior. For a smooth user
experience, the program must display clear prompts and messages,
indicating the result of each operation.
This project demonstrates the practical use of circular queues in real-world
systems and enhances understanding of key programming concepts like
dynamic memory allocation, modular arithmetic, and efficient data
manipulation. By solving this problem, users gain insight into the importance
of circular queues and their ability to optimize resource utilization in
constrained environments.

Flowchart
CODE

#include <iostream>

using namespace std;

class CircularQueue {

private:

int front, rear, size;

int *queue;

public:

CircularQueue(int s) {

size = s;

queue = new int[size];

front = -1;

rear = -1;

~CircularQueue() {

delete[] queue;

// Function to add an element to the queue


void enqueue(int value) {

if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) {

cout << "Queue is Full! Cannot add more elements.\n";

return;

if (front == -1) { // First element

front = rear = 0;

} else if (rear == size - 1 && front != 0) {

rear = 0; // Wrap around

} else {

rear++;

queue[rear] = value;

cout << value << " added to the queue.\n";

// Function to remove an element from the queue

void dequeue() {

if (front == -1) {

cout << "Queue is Empty! Nothing to remove.\n";

return;

cout << "Removed: " << queue[front] << endl;


if (front == rear) {

// Only one element was present

front = rear = -1;

} else if (front == size - 1) {

// Wrap around

front = 0;

} else {

front++;

// Function to display the queue elements

void display() {

if (front == -1) {

cout << "Queue is Empty!\n";

return;

cout << "Queue elements are: ";

if (rear >= front) {

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

cout << queue[i] << " ";

} else {

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

cout << queue[i] << " ";

}
for (int i = 0; i <= rear; i++) {

cout << queue[i] << " ";

cout << endl;

};

int main() {

int size, choice, value;

cout << "Enter the size of the Circular Queue: ";

cin >> size;

CircularQueue cq(size);

while (true) {

cout << "\nMenu:\n";

cout << "1. Enqueue\n";

cout << "2. Dequeue\n";

cout << "3. Display Queue\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:
cout << "Enter the value to enqueue: ";

cin >> value;

cq.enqueue(value);

break;

case 2:

cq.dequeue();

break;

case 3:

cq.display();

break;

case 4:

cout << "Exiting program. Goodbye!\n";

return 0;

default:

cout << "Invalid choice. Please try again.\n";

return 0;

You might also like