Data Str & Algorithm HW Solutions 77

Data Str & Algorithm HW Solutions 77 - 4 pointers,...

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

View Full Document Right Arrow Icon
77 update[i] = x; // Keep track of end at level i } if (!KEcomp.eq(K, x->forward[0]->value)) return; // Value not in list x = forward[0]; // Pointing at node to delete for (i=0; i<=x->level; i++) // Fix up the pointers update[i]->forward[i] = x->forward[i]; Elem temp = x->value; delete x; return temp; } 12.5 This is something of a trick question. There is no good access method for f nding the i th node, other than to count over i pointers at level 0. template <class Key, class Elem, class KEComp, class EEComp> SkipNode<Elem>* SkipList::ithnode(int i) { SkipNode<Elem>* curr = head; for(int j=0; j<i; j++) { if (curr->forward[0] == NULL) return NULL; // No ith node in list curr = curr->forward[0]; } return curr; } 12.6 A regular array cell requires 8 bytes (a value. A sparse matrix cell requires
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4 pointers, two indices and a value for a total of 28 bytes. If the array con-tains more than 8/28 or 29% non-zero-valued elements, then the regular array representation will be more space ef f cient. Note that this ignores the space required for the row and column headers, which will be 10( M + N ) for and M N matrix. 12.7 \\ Written so that tail returns the tail of the list MLnode* reverse(MLnode* rt) { if (rt == NULL) return NULL; rt-&gt;child = reverse(rt-&gt;child); if (rt-&gt;next == NULL) return rt; // Only elem on list MLnode* newrt = reverse(rt-&gt;next); rt-&gt;next-&gt;next = rt; // rt-&gt;next still points at // original next node // (which is now tail of // reversed list) rt-&gt;next = NULL; return newrt;...
View Full Document

Ask a homework question - tutors are online