Data Str & Algorithm HW Solutions 78

Data Str & Algorithm HW Solutions 78 -...

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

View Full Document Right Arrow Icon
78 Chap. 12 Lists and Arrays Revisited } 12.8 void SparseMatrix insert(int r, int c, int val) { for (SMhead* crow = row; (crow->index <= r) & (crow->next != NULL); crow = crow->next); // First, find the row if (crow->index != r) // Make a new row crow->next = new SMhead(r, crow->next, NULL); for (SMhead* ccol = col; (ccol->index <= c) & (ccol->next != NULL); ccol = ccol->next); // Now, find the column if (ccol->index != c) // Make a new row ccol->next = new SMhead(c, ccol->next, NULL); // Now, put in its row; if ((crow->first == NULL) || (crow->first->col > c)) SMElem* temp = crow->first = new SMElem(val, crow->first, NULL, NULL, NULL); else { for (SMElem* temp = crow->first;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (temp-&amp;gt;col &amp;lt;= col) &amp;amp;&amp;amp; (temp-&amp;gt;nextcol != NULL); temp = temp-&amp;gt;nextcol); if (temp-&amp;gt;col == c) { // Replace entry value temp-&amp;gt;value = val; return; } temp-&amp;gt;nextcol = new SMElem(val, temp-&amp;gt;nextcol, temp, NULL, NULL); temp = temp-&amp;gt;nextcol; temp-&amp;gt;nextcol-&amp;gt;prevcol = temp; } // Finally, put in its column; if ((ccol-&amp;gt;first == NULL) || (ccol-&amp;gt;first-&amp;gt;row &amp;gt; c)) { temp-&amp;gt;nextrow = ccol-&amp;gt;first; temp-&amp;gt;prevcol = NULL; ccol-&amp;gt;first = temp; } else { for (SMElem* tempc = ccol-&amp;gt;first; (tempc-&amp;gt;row &amp;lt;= row) &amp;amp;&amp;amp; (tempc-&amp;gt;nextrow != NULL); tempc = tempc-&amp;gt;nextrow); temp-&amp;gt;prevrow = tempc; temp-&amp;gt;nextrow = tempc-&amp;gt;nextrow; tempc-&amp;gt;nextrow-&amp;gt;prevrow = temp;...
View Full Document

This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online