Data Str & Algorithm HW Solutions 80

Data Str & Algorithm HW Solutions 80 - for (each...

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

View Full Document Right Arrow Icon
80 Chap. 12 Lists and Arrays Revisited // For each element, switch row and column pointers, // and its row/column indices. for (each row) for (each element in the row) { swap(nextrow, nextcol); swap(prevrow, prevcol); swap(row, col); } swap(row, col); // Swap row and column list headers } 12.11 Here is pseudocode to add two sparse matrices. // The current object will be the result of adding // its two inputs. SMhead *temp1 = In1->row; SMhead *temp2 = In2->row; while ((temp1 != NULL) || (temp2 != NULL)) if ((temp2 == NULL) || (temp1->index < temp2->index)) { // Insert row from first matrix
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for (each elem in temp1s row) this.insert(elem-&gt;value, elem-&gt;row, elem-&gt;col); } else if ((temp1==NULL) || (temp2-&gt;index &lt; temp1-&gt;index)) { // Insert row from second matrix for (each elem in temp2s row) this.insert(elem-&gt;value, elem-&gt;row, elem-&gt;col); } else { // Both matrices have this row SMElem* curr1 = temp1-&gt;first; SMElem* curr2 = temp2-&gt;first; if ((curr2 == NULL) || (curr1-&gt;col &lt; curr2-&gt;col)) { // Insert element from first matrix this.insert(curr1-&gt;value, curr1-&gt;row, curr1-&gt;col); curr1 = curr1-&gt;nextcol; } else if ((curr1 == NULL) || (curr2-&gt;col &lt; curr1-&gt;col)) {...
View Full Document

Ask a homework question - tutors are online