Question

In: Computer Science

C++ Project The following is a simple implementation of the circular queue Important: for this project,...

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

}

Solutions

Expert Solution

/*** 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]<<" "

    }

}


Related Solutions

Implementation of a a queue with a "circular" array or a "regular" array. (a) "circular" array:...
Implementation of a a queue with a "circular" array or a "regular" array. (a) "circular" array: consider using the smallest number of fields/variables in a struct/class. List the fields/variables that support the enqueue and dequeue operations in O(1) time in the worst case. Explain why the list is the smallest and the two operations are O(1) time. (b) "regular" array: explain the worst-case time complexity in big-O for the operations (queue size is at most n).
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue <T>{ } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a heap using namespace std; /* In this lab you will implement a data structure that supports 2 primary operations: - insert a new item - remove and return the smallest item A data structure that supports these two operations is called a "priority queue". There are many ways to implement a priority queue, with differernt efficiencies for the two primary operations. For this lab,...
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (explain briefly in java)
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (In java)
Please complete it in C++ Part 2: Queue Create a New Project and give your project...
Please complete it in C++ Part 2: Queue Create a New Project and give your project a name, say Lab6b. Download the given source files StackArr.h, StackArr.cpp, QueueArr.h and QueueArr.cpp from Canvas and save them to your Lab6b folder. Also import them to your project. Add a source file to your project, called QueueMain.cpp and implement your program according to the following: prompt the user to input a string; change each uppercase letter to lowercase; place each letter both in...
2. The circular-flow model The following diagram presents a circular-flow model of a simple economy. The...
2. The circular-flow model The following diagram presents a circular-flow model of a simple economy. The outer set of arrows (shown in green) shows the flow of dollars, and the inner set of arrows (shown in red) shows the corresponding flow of inputs and outputs.    Based on this model, households earn income when   purchase   in factor markets. Suppose Frances earns $700 per week working as a corporate attorney for Rowan and Martin Associates. She uses $9 to order a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT