Question: Modify the code of Figure 7.3 or your solution to Exercise 7.12 to throw an exception if an attempt is made to enqueue an item
Modify the code of Figure 7.3 or your solution to Exercise 7.12 to throw an exception if an attempt is made to enqueue an item in a full queue, or to dequeue an item from an empty queue.
Data From Exercise 7.12:
Figure 7.3 passes integer max_items to the queue abstraction as a generic parameter. Write an alternative version of the code that makes max_items a parameter to the queue constructor instead. What is the advantage of the generic parameter version?
Figure 7.3:
![template class queue { item items [max_items]; int next_free, next_full, num_items; public: queue () : next_free(0), next_full(0), num_items (0) { } bool enqueue (const item& it) { if (num_items == max_items) return false; ++num_items; items [next_free] it; next_free (next_free + 1) % max_items; %3D return true; bool dequeue (item* it)](https://dsd5zvtm8ll6.cloudfront.net/si.question.images/images/question_images/1606/2/0/5/4025fbcbfda669561606205402828.jpg)
template class queue { item items [max_items]; int next_free, next_full, num_items; public: queue () : next_free(0), next_full(0), num_items (0) { } bool enqueue (const item& it) { if (num_items == max_items) return false; ++num_items; items [next_free] it; next_free (next_free + 1) % max_items; %3D return true; bool dequeue (item* it) { if (num_items 0) return false; *it = items [next_full]; --num_items; next_full = (next_full + 1) % max_items; %3D return true; }; queue ready_list; queue int_queue;
Step by Step Solution
3.42 Rating (158 Votes )
There are 3 Steps involved in it
class queueoverflow class queueunderflow template class queue item items maxitems in... View full answer
Get step-by-step solutions from verified subject matter experts
