Challenge 51b

Description

The purpose of this challenge is to implement a circular queue (ring buffer) using an array as a backing data structure

Requirements

  1. Create a class RingBuffer
  2. Create private variables: int size; string * names; int front; int back. When the queue is not empty, remember that back points to the next available location to which data can be enqueued, and front points to the location from which data can be dequeued.
  3. Create an overloaded constructor that passes in an integer. This integer will be used to set the size value. Initialize names as a dynamic array of string of size. Set front and back to 0. Set the entire names array to set each element to each be a blank string.
  4. Don’t forget to write a destructor to delete the names array
  5. Write a private function is_empty(). This function returns true if front == back, false otherwise.
  6. Write a private function is_full(). This function returns true if ((back + 1) % size) == front. IMPORTANT: Our definition of a full queue using this formula will actually leave one last cell of the array unusable. This means, if an array is defined with a size of 10, then only 9 elements will be usable. 
  7. Write a public function bool enqueue(string name). This function returns true if name is successfully added to the queue and false otherwise. If the queue is not full:
    1. add name into the names  array at index back
    2. increment back with modulus against size. This creates the looping behavior.
      back = (back + 1) % size
      
  8. Write a public function string dequeue(). This function :
    1. Will return a blank string if the queue is empty
    2. Will check to see that the queue is not empty and extract the element of the names array at position front. The extracted data is returned from the function.
    3. Will increment front with modulus against size. This preserves the looping behavior, similar to how back is incremented.
  9. Write a function friend void show(RingBuffer & rb). This will show the contents of actual queue contents, not the names array (which shows all values, some of which may or may not be actual queue items). Use a while loop that starts from front and loops to back. Use a counter that is initialized to front and advances as long at it is not equal to back (We check for inequality not less-than, because checking for less-than will fail at the border between the last element and the first element). Remember to advance and mod by size.
  10. Use the main() given below

Sample main()

int main()
{
  RingBuffer buffer(4);
  char action;
  string name = "";
  bool added;

  do
  {
    cout << "e - Enqueue" << endl;
    cout << "d - Dequeue" << endl;
    cout << "q - Quit" << endl;
    cout << "Action: ";
    cin >> action;

    if (action == 'e')
    {
      cout << "Name: ";
      cin >> name;
      if (buffer.enqueue(name))
      {
        cout << "Added " << name << endl;
      }
      else
      {
        cout << name << " was not added. Queue full" << endl;
      }
    } else if (action == 'd')
    {
      name = buffer.dequeue();
      if (name == "")
      {
        cout << "Queue is empty" << endl;
      }
      else
      {
        cout << "Dequeued " << name << endl;
      }
    }
    show(buffer);
    cout << "-------------" << endl;

  } while (action != 'q');
  return 0;
}

CATALOG ID: CPP-CHAL00051b

Print Requirements