Question

Balment a la medicul Quoc that speciala a circular que has the following private data members and public member functions. Th
Removes them at the front of the queue. The tum vale is the element that was removed. If the queue is empty then you should f
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<stdlib.h>
using namespace std;

// Class CircularQueue definition
class CircularQueue
{
// an index representing front and back for the queue
int backIndex, frontIndex;

// Represents the capacity of the array
int capacity;
// Represents the actual number of elements in the queue
int currentSize;
// Array to store data
int *theArray;

public:
// Prototype of member functions
CircularQueue(int cap = 10);
~CircularQueue();
void enqueue(int);
int dequeue();
int getCapacity() const;
int getSize() const;
bool isEmpty() const;
bool isFull() const;
int getFront() const;
int getBack() const;
void displayqueue();
};// End of class

// Destructor
CircularQueue::~CircularQueue()
{
// Deletes the array
delete theArray;
// Initializes front and back index to -1
frontIndex = backIndex = -1;
// Initializes current size to 0
currentSize = 0;

}
// Parameterized constructor definition takes cap as parameter
CircularQueue::CircularQueue(int cap)
{
// Initializes front and back index to -1
frontIndex = backIndex = -1;
// Initializes current size to 0
currentSize = 0;
// Assigns parameter cap to data member capacity
capacity = cap;
// Dynamically allocates memory to pointer theArray of capacity parameter value cap
theArray = new int[cap];
}// End of parameterized constructor

// Function to return capacity of the circular queue
int CircularQueue::getCapacity() const
{
return capacity;
}// End of function

// Function to return current size of the circular queue
int CircularQueue::getSize() const
{
return currentSize;
}// End of function

// Function to return true if circular queue is empty otherwise returns false
bool CircularQueue::isEmpty() const
{
return (currentSize == 0);
}// End of function

// Function to return true if circular queue is full otherwise returns false
bool CircularQueue::isFull() const
{
return (currentSize == capacity);
}// End of function

// Function to return the front element
int CircularQueue::getFront() const
{
return theArray[frontIndex];
}// End of function

// Function to return the back element
int CircularQueue::getBack() const
{
return theArray[backIndex];
}// End of function

// Function to inset number to Circular Queue
void CircularQueue::enqueue(int value)
{
// Checks circular queue is full or not

// Checks if front index is zero and back index is capacity minus one
// or back index is equals to front index -1 module capacity minus one remainder
// Then display queue is full
if ((frontIndex == 0 && backIndex == capacity - 1) || (backIndex == (frontIndex - 1) % (capacity - 1)))
{
cout<<"\n Queue is full.";
return;
}// End of if condition

// Insert First Element
// Checks if front index is -1 no elements available in circular queue
else if (frontIndex == -1)
{
// Both front and back index is initializes to zero
frontIndex = backIndex = 0;
// Assigns value to the back index position of array theArray i.e., rear of the circular queue
theArray[backIndex] = value;
// Increase the current counter by one
currentSize++;
}// End of else if condition

// Last number to insert
// Checks if back index is equals to capacity -1 and front index is not zero
else if (backIndex == capacity - 1 && frontIndex != 0)
{
// Reset the back index to zero to point to beginning of the array
backIndex = 0;
// Assigns value to the back index position of array theArray i.e., rear of the circular queue
theArray[backIndex] = value;
// Increase the current counter by one
currentSize++;
}// End of else if condition

// Otherwise
else
{
// Increase the back index position by one to point to next index position
backIndex++;
// Assigns value to the back index position of array theArray i.e., rear of the circular queue
theArray[backIndex] = value;
// Increase the current counter by one
currentSize++;
}// End of else
}// End of function

// Function to delete element from Circular Queue
int CircularQueue::dequeue()
{
// Checks Circular Queue is empty or not

// Checks if front index is -1 then display circular queue is empty
if (frontIndex == -1)
{
cout<<"\n Queue is empty";
return INT_MIN;
}// End of if condition

// Otherwise assigns front index position data of array number to data
int data = theArray[frontIndex];
// Assigns -1 to front index position of the array number to -1
theArray[frontIndex] = -1;

// Decrease the current size by one
currentSize--;

// Only one number to delete

// Checks if front and back index value is same
if (frontIndex == backIndex)
{
// Reset the front and back index to -1 for empty circular queue
frontIndex = backIndex = -1;
}// End of if condition

// Last element to delete

// Otherwise checks if front index is equals to capacity -1 reset the front index to 0
// To point to beginning of the array
else if (frontIndex == capacity - 1)
frontIndex = 0;
// Otherwise increase the front index by one to point to next index position of the array
else
frontIndex++;

// Returns the deleted data
return data;
}// End of function

// Function displaying the elements of Circular Queue
void CircularQueue::displayqueue()
{
// Checks Circular Queue is empty or not

// Checks if front index is -1 then display circular queue is empty
if (frontIndex == -1)
{
cout<<"\n Queue is empty";
return;
}// End of if condition

cout<<"\n Elements in Circular Queue: ";

// Checks if back index value is greater than or equals to front index
if (backIndex >= frontIndex)
{
// Loops from front index to back index and displays the values
for (int c = frontIndex; c <= backIndex; c++)
cout<<theArray[c]<<" -> ";
}// End of if condition
// Otherwise
else
{
// Loops from front index to end of circular queue and displays the values
for (int c = frontIndex; c < capacity; c++)
cout<<theArray[c]<<" -> ";
// Loops from zero index to back index and displays the values
for (int c = 0; c <= backIndex; c++)
cout<<theArray[c]<<" -> ";
}// End else
}// End of function

// Function to display menu, accept user choice and returns it
int menu()
{
// To store user choice
int ch;
cout<<"\n\n ************************ Circular Queue Menu ************************";
// Displays menu
cout<<"\n\t 1 - Insert. \n\t 2 - Delete. \n\t 3 - Display. \n\t 4 - Check Empty. \n\t 5 - Check Full."
<<"\n\t 6 - Get Front Element. \n\t 7 - Get Back Element. \n\t 8 - Get Capacity \n\t 9 - Get Size \n\t 10 - Exit.";
// Accepts user choice
cout<<"\n\t\t What is your choice? ";
cin>>ch;
// Returns user choice
return ch;
}// End of function

// main function definition
int main()
{
// To store the size entered by the user
int si;
// To store the number entered by the user
int no;
// Accepts circular queue size from user
cout<<"\n Enter the size of the Circular Queue: ";
cin>>si;

// Creates an object using parameterized constructor
CircularQueue myqueue(si);

// Loops till user choice is not 4
do
{
// Calls the function to display menu
// Based on the return value calls the function
switch(menu())
{
case 1:
cout<<"\n Enter a number to insert: ";
cin>>no;
myqueue.enqueue(no);
break;
case 2:
no = myqueue.dequeue();
if(INT_MIN != no)
cout<<"\n Deleted number: "<<no;
break;
case 3:
myqueue.displayqueue();
break;
case 4:
if(myqueue.isEmpty())
cout<<"\n Empty Circular Queue.";
else
cout<<"\n Not Empty Circular Queue.";
break;
case 5:
if(myqueue.isFull())
cout<<"\n Full Circular Queue.";
else
cout<<"\n Not Full Circular Queue.";
break;
case 6:
cout<<"\n Front Element of Circular Queue: "<<myqueue.getFront();
break;
case 7:
cout<<"\n Back Element of Circular Queue: "<<myqueue.getBack();
break;
case 8:
cout<<"\n Capacity of Circular Queue: "<<myqueue.getCapacity();
break;
case 9:
cout<<"\n Number of Elements in Circular Queue: "<<myqueue.getSize();
break;
case 10:
exit(0);
default:
cout<<"\n Invalid Choice! Try again.";
}// End of switch - case
}while(1); // End of do - while loop
return 0;
}// End of main function

Sample Output:

Enter the size of the Circular Queue: 10


************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 1

Enter a number to insert: 10


************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 1

Enter a number to insert: 20


************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 1

Enter a number to insert: 30


************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 3

Elements in Circular Queue: 10 -> 20 -> 30 ->

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 2

Deleted number: 10

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 4

Not Empty Circular Queue.

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 5

Not Full Circular Queue.

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 6

Front Element of Circular Queue: 20

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 3

Elements in Circular Queue: 20 -> 30 ->

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 7

Back Element of Circular Queue: 30

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 8

Capacity of Circular Queue: 10

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 9

Number of Elements in Circular Queue: 2

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 11

Invalid Choice! Try again.

************************ Circular Queue Menu ************************
1 - Insert.
2 - Delete.
3 - Display.
4 - Check Empty.
5 - Check Full.
6 - Get Front Element.
7 - Get Back Element.
8 - Get Capacity
9 - Get Size
10 - Exit.
What is your choice? 10

Add a comment
Know the answer?
Add Answer to:
Balment a la medicul Quoc that speciala a circular que has the following private data members...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. 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. Efficiently implement a queue class using a circular...

  • Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front =...

    Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front = 0, back = 0; void enqueue(int x) { data[back] = x; back = (back + 1) % 6; } void dequeue() { front = (front + 1) % 6; } and we perform the following series of queue operations: enqueue(1); dequeue(); enqueue(2); dequeue(); enqueue(7); enqueue(3); enqueue(5); dequeue(); dequeue(); enqueue(4); enqueue(6); Write the state of the queue array after each operation, and at the end,...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Breadth First Search algorithm, process scheduling, graph theory, and congestion handling in networks are some applications...

    Breadth First Search algorithm, process scheduling, graph theory, and congestion handling in networks are some applications of queues. The two core operations in queues include enqueue () for insertions on one end, and dequeue () for deletions from the other end, size() returns the size of the queue, front() returns the first element in the queue, empty() determines if the queue is empty. Considering the lines give below, write a c program that implements enqueue and dequeue operations and print...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT