Sortinganexistinglikedlist

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ink = Temp->fLink; Temp->MPGbLink = Temp->bLink; } while (Temp != MPGlast) The linked list is now set and ready to be sorted with respect to MPG in descending order. Sorting an existing liked list: Interestingly, sorting function for the MPG linked list takes some thinking. I suggest the following code but expecting perhaps more efficient or at least shorter code from you. // Sort the list with respect to decreasing MPG. tEmp = MPGfirst; while ((tEmp->MPGfLink != MPGlast)&&(tEmp->MPGfLink != NULL)) { Temp = tEmp->MPGfLink; While ((tEmp->MPG >= Temp->MPG)&&(Temp->MPGfLink != NULL)) Temp = Temp->MPGfLink; // Eight pointers need to be adjusted. tEmp->MPGbLink->MPGfLink = Temp; Temp->MPGbLink->MPGfLink = tEmp; tEmp->MPGfLink->MPGbLink = Temp; Temp->MPGfLink->MPGbLink = tEmp; // Swapping the pointers // Storing the links teMp = tEmp->MPGfLink; temP = tEmp->MPGbLink; tEmp->MPGfLink = Temp->MPGfLink; tEmp->MPGbLink = Temp->MPGbLink; Temp->MPGfLink = teMp; Temp->MPGbLink = temP; tEmp = teMp; } // Move on to next...
View Full Document

This document was uploaded on 02/17/2014 for the course ECET 370 at DeVry Long Beach.

Ask a homework question - tutors are online