In: Computer Science
C++ Project
The following is a simple implementation of the circular queue
Important: for this project, you must complete the work based on this initial code.
1. Fix the full() method.
2. Fix the empty() method.
3. Place "X" in the front position using a statement in the constructor.
4. Adjust the DEQ() method so that the front position in the array would always have "X" in it and
the previous X should be blanked out.
5. Write the code for displayArray() method in a such a way that it should display the contents of the
Queue as shown in the array, from location 0 up to and including the last position.
6. Write the code for displayLine() method in a such a way that it should display the contents of the
Queue, from the rear position to the front position as in a waiting line.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include
using namespace std;
class Queue{
private:
int front;
int rear;
int capacity;
string *array;
public:
Queue(int capacity){
// Place "X" in the front position
front = capacity;
rear = capacity;
(*this).capacity = capacity;
array = new string[capacity + 1];
}
/*** You need to fix the method full, so that it would only return
* true if front and rear have the same value.
*/
bool full(){
return false;
}
void ENQ(string element){
if (!full()){
if (rear == 0){
rear = capacity;
}
else{
rear--;
}
array[rear] = element;
}
else{
cout<<"Queue is full "<
}
}
/**
* You need to fix the method empty, so that it would only return
* true if the next rear position is the same as the front position.
*/
bool empty(){
return false;
}
/**
* Adjust DEQ so that, the front position in the array would
* always have "X" in it and the previous X should be blanked out.
*/
string DEQ(){
if (!empty()){
if (front == 0){
front = capacity;
}
else{
front--;
}
return array[front];
}
else{
cout<<"Queue is empty\n";
return "Nothing";
}
}
/**
* Write the code for displayArray() method in a such a way that it should display the contents
* of the Queue as shown in the array from location 0 up to and including the last position.
void displayArray(){
}
/**
* Write the code for displayLine() method in a such a way that it should display the contents of the
* Queue from the rear position to the front position as in a waiting line.
*/
void displayLine(){
}
};
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Main Code
int main() {
cout<<"\nTesting Q1 ...\n";
Queue Q1(5);
//displayArray(); test, once it is done
Q1.ENQ("B");
//displayArray(); test, once it is done
Q1.ENQ("D");
//displayArray(); test, once it is done
Q1.ENQ("C");
//displayArray(); test, once it is done
cout<
Q1.ENQ("P");
//displayArray(); test, once it is done
Q1.ENQ("H");
//displayArray(); test, once it is done
Q1.ENQ("N");
//Q1.displayArray(); test, once it is done
//Test if your full method works since M should be rejected
//Q1.ENQ("M")
cout<
//Q1.displayArray(); test, once it is done
cout<
//Q1.displayArray(); test, once it is done
//Q1.displayArray(); test, once it is done
cout<<"\nTesting Q2 ...\n";
Queue Q2(6);
Q2.ENQ("H");
Q2.ENQ("P");
Q2.ENQ("M");
Q2.ENQ("W");
Q2.ENQ("Z");
Q2.ENQ("N");
//Test if your full method works since Z should be rejected
//Q2.ENQ("Z");
//Q2.displayArray(); test once it is done
for (int i = 1; i<=6; i++){
cout<
}
//Test if your full method works since M should be rejected
cout<
//Q2.displayArray(); test, once it is done
//Q2.displayArray(); test, once it is done
}
/*** fix of the method full, so that it would only return
* true if front and rear have the same value.
*/
bool ful()
{
//checking if the value of front and rear is equal then it will return true
if(array[front]==array[rear]) return true
return false
}
/**
* fix of the method empty, so that it would only return
* true if the next rear position is the same as the front position.
*/
bool empty()
{
//checking if the front position is equal to next rear position then returning true
if(front == rear+1) return true
return false
}
/**
* code for displayArray() method in a such a way that it should display the contents
* of the Queue as shown in the array from location 0 up to and including the last position.
/**
void displayArray()
{
//checking if queue is empty or not
if (empty())
{
cout("Queue is Empty");
return;
}
cout("Elements in Circular Queue are: ");
//if rear position is greater than and equal to front then simply printing
// the queue from front to rear using for loop starting from front to rear
if (rear >= front)
{
for (int i = front; i <= rear; i++)
cout<<array[i]<<" "
}
// else if rear position is smaller to front then first print the queue element from
// front to max size of array ie. capacity of array and then print the queue elelemt
// form 0 index to rear position
else
{
// finding the size of array
int array_size = sizeof(array)/sizeof(array[0]);
for (int i = front; i < array_size; i++)
cout<<array[i]<<" "
for (int i = 0; i <= rear; i++)
cout<<array[i]<<" "
}
}
/**
* code for displayLine() method in a such a way that it should display the contents of the
* Queue from the rear position to the front position as in a waiting line.
*/
void void displayLine()
{
//checking if queue is empty or not
if (empty())
{
cout("Queue is Empty");
return;
}
cout("Elements in Circular Queue are: ");
//if rear position is greater than and equal to front then simply printing
// the queue from rear to front using for loop starting from rear to front
if (rear >= front)
{
for (int i = rear; i <= front; i--)
cout<<array[i]<<" "
}
// else if rear position is smaller to front then first
// print the queue element form rear position 0 index
// then max size of array ie. capacity of array to front of the queue elelemt
else
{
for (int i = rear; i >= 0; i--)
cout<<array[i]<<" "
// finding the size of array
int array_size = sizeof(array)/sizeof(array[0]);
for (int i = array_size-1; i >= front; i--)
cout<<array[i]<<" "
}
}