4.6 template void LList::reverse(){ if (head->next == NULL) return; if (curr->next == NULL) curr = head; else curr = curr->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.8 (a) There are ways to implement a circular linked list without head and tail pointers. But since we're modifying the class LList, we may as well use them. template void LList::LList(const int sz) { head = tail = curr = new Link; // Create header head->next = head; } template void LList::clear() { // Remove Elements while (head->next != NULL) { // Return to free curr = head->next; // (keep header) head->next = curr->next; delete curr; } tail = curr = head->next = head; // Reinitialize } // Insert Element at current position template void LList::insert(const E& item) { assert(curr != NULL); // Must be pointing to Element curr->next = new Link(item, curr->next); if (tail->next != head) tail = tail->next; } template // Put at tail void LList::append(const E& item) { tail = tail->next = new Link(item, head); } // Move curr to next position template void LList::next() { curr = curr->next; } // Move curr to prev position template void LList::prev() { Link* temp = curr; while (temp->next!=curr) temp=temp->next; curr = temp; } (b) is similar. 4.12 Let's assume an int requires 4 bytes, a double requires 8 bytes, and a pointer requires 4 bytes. The same ideas work if your computer has different sizes. Using the equation on p. 113: (a) Since E = 4 and P = 4, the break-even point occurs when n = 4D/8 = D/2. Thus, the linked list is more space efficient when the array would be less than half full. (b) Since E = 8 and P = 4, the break-even point occurs when n = 8D/12 = 2D/3. Thus, the linked list is more space efficient when the array would be less than two thirds full. 4.16 using namespace std; #include "astack.h" int fibs(int n) { AStack S; // The following three variables are stored on the stack for each // recursive call to fibr: // branch keeps track of the number (0, 1, or 2) of times fibr has been // in one recursion. // arg is the value passed to fibr // val is the value of fibr computed in one recursion int branch, arg, val, temp; if(n <= 2) return 1; // Initialize stack S.push(0); S.push(n); S.push(0); while(true) { branch = S.pop(); arg = S.pop(); val = S.pop(); if (branch == 0) { if (arg <= 2) { S.push(1); S.push(arg); S.push(1); } else { S.push(val); S.push(arg); S.push(1); S.push(0); S.push(arg-1); S.push(0); } } if (branch == 1) { if (arg <= 2) { S.push(1); S.push(arg); S.push(2); } else { S.push(val); S.push(arg); S.push(2); S.push(0); S.push(arg-2); S.push(0); } } if (branch == 2) { if (S.length() == 0) { return val; } else { branch = S.pop(); arg = S.pop(); temp = S.pop(); S.push(val + temp); S.push(arg); S.push(branch); } } } } 4.18 void reverse(Queue& Q, Stack& S) { E X; while (!Q.isEmpty()) { X = Q.dequeue(); S.push(X); } while (!S.isEmpty()) { X = S.pop(); Q.enqueue(X); } }