Question: 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
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/2985fbcbf725de8b1606205298225.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.44 Rating (163 Votes )
There are 3 Steps involved in it
NB Like Figure 73 this code does not check for overflow or underflow The advanta... View full answer
Get step-by-step solutions from verified subject matter experts
