Exercises from Chapter 4 4.4 4.5 4.6 4.8 4.9 Solutions 4.4 // returns false if right partition is empty; // if right partition has only one member, it is swapped with itself bool interchange(){ Elem temp; if (!remove(temp)) return false; next(); insert(temp); return true; }; 4.5 template void LList::reverse(){ if (head->next == NULL) return; if (fence->next == NULL) fence = head; else fence = fence->next; link* temp1 = head->next; link* temp2 = temp1->next; while (temp2 != NULL) { link* temp3 = temp2->next; temp2->next = temp1; temp1 = temp2; temp2 = temp3; } head->next = temp1; } 4.6 (a) The idea is that there is no first or last link. So there won't be head or tail pointers, left or right partitions, or a dummy node. You could have instead of leftcnt and rightcnt an int cnt whose value is the number of links in the list. Fence is still used to point to the link where new links are inserted or removed. Here are some illustrations of functions. template void LList::LList(const int size) { fence = NULL; cnt = 0; } template bool LList::insert(const Elem& item) { if (fence == NULL) {fence = new Link(item); fence->next = fence;} else fence->next = new Link(item,fence->next); cnt++; return true; } template bool LList::remove(const Elem& item) { if (fence == NULL) return false; if (fence->next == fence) {item = fence->element; delete fence; fence = NULL;} else { item = fence->next->element; Link* ltemp = fence->next; fence->next = ltemp->next; delete ltemp; } cnt--; return true; } template void LList::prev() { if (fence == NULL) return; Link* temp = fence; while (temp->next != fence) temp = temp->next; fence = temp; } // Since there is no end of list, you can't append items to the // end of the list. But if you don't want to take the easy way out, // define append to mean insert just before the fence. template bool LList::append(const Elem& item) { prev(); bool res = insert(item); next(); return res; } (b) Similar. 4.8 D and n are numbers of elements. P and E are in units of computer memory, say bytes. Therefore the left side of the inequality n > DE/(P+E) is in numbers of elements, and the right side is in (numbers of elements)*bytes/bytes, which is also in numbers of elements. So the two sides balance in terms of dimensional units. 4.9 (a) The break-even point is when n = 20*8/(4+8) = 13.333... (b) n = 30*2/(4+2) = 10 (c) n = 30*1/(4+1) = 6 (d) n = 40*32/(4+32) = 35.555...