Data Str & Algorithm HW Solutions 24

Data Str & Algorithm HW Solutions 24 - } tail =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
24 Chap. 4 Lists, Stacks, and Queues link<Elem>* temp1 = head->next; link<Elem>* temp2 = temp1->next; while (temp2 != NULL) { link<Elem>* temp3 = temp2->next; temp2->next = temp1; temp1 = temp2; temp2 = temp3; } head->next = temp1; } 4.6 (a) The following members are modi f ed. template <class Elem> void LList<Elem>::LList(const int sz) { head = tail = curr = new link; // Create header head->next = head; } template <class Elem> void LList<Elem>::clear() { // Remove Elems while (head->next != NULL) { // Return to free curr = head->next; // (keep header) head->next = curr->next; delete curr;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } tail = curr = head-&gt;next = head; // Reinitialize } // Insert Elem at current position template &lt;class Elem&gt; void LList&lt;Elem&gt;::insert(const Elem&amp; item) { assert(curr != NULL); // Must be pointing to Elem curr-&gt;next = new link(item, curr-&gt;next); if (tail-&gt;next != head) tail = tail-&gt;next; } template &lt;class Elem&gt; // Put at tail void LList&lt;Elem&gt;::append(const Elem&amp; item) { tail = tail-&gt;next = new link(item, head); } // Move curr to next position template &lt;class Elem&gt; void LList&lt;Elem&gt;::next() { curr = curr-&gt;next; }...
View Full Document

Ask a homework question - tutors are online