Connecting Tech Pros Worldwide Forums | Help | Site Map

sorting in link list

Newbie
 
Join Date: Sep 2006
Location: south africa
Posts: 7
#1: Sep 28 '06
Question is :
Write a program making use of link list class
-create a class for details of staff( name ,surname and registration number)
-sort each staff according to registration number {***im having a problem with this part}

please help...

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,190
#2: Sep 28 '06

re: sorting in link list


Sorting is relatively easy. Sort the list entries as you add them to the list. Rather than add the new item at the head of the list search down the list to find the item you want to add the new entry after then

newEntry->next = currentEntry->next
currentEntry->next = newEntry

You will need a special case to handle needing to add the new entry at the head of the list (an alternitive to a special case is to uses pointers to pointers but I find a special case easier to read and understand)..
Reply