Description
The purpose of this challenge is to implement a circular queue (ring buffer) using an array as a backing data structure
Requirements
- Create a class RingBuffer
- Create private variables: int size; string * names; int head; int tail
- 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 head and tail to -1. Set the entire names array to set each element to each be a blank string.
- Don’t forget to write a destructor to delete the names array.
- Write a private function is_empty(). This function returns true if head == -1, false otherwise.
- Write a private function is_full(). This function returns true if head == 0 and tail == size-1, OR head == tail + 1.
- 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:
- Set head to 0 if head is -1
- increment tail forward. Remember to mod by size.
- add name into the names array at index tail
- Write a public function string dequeue(). This function :
- Will return a blank string if the queue is empty
- Will check to see that the queue is not empty and extract the element of the names array at position head.
- Will reset head and tail to -1 if head == tail. Otherwise, it will advanced head forward. Remember to mod by size, realizing that as head advances, it can wrap around to a smaller value
- 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 head and loops to tail. Use a counter that is initialized to head and advances as long at it is not equal to tail. Remember to advance and mod by size. Immediately after the while loop, you will have to show names[i] at the current value of i to account for the last item.
- Use the main() given below
Sample main()
int main() { RingBuffer buffet(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 (buffet.enqueue(name)) { cout << "Added " << name << endl; } } else if (action == 'd') { name = buffet.dequeue(); if (name != "") { cout << "Dequeued " << name << endl; } } show(buffet); cout << "-------------" << endl; } while (action != 'q'); return 0; }
CATALOG ID: CPP-CHAL00051
Print Requirements