Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   problem implementing- printing sorted linked list (http://www.go4expert.com/forums/implementing-printing-sorted-linked-list-t17262/)

sucre 1May2009 15:00

problem implementing- printing sorted linked list
 
Hello,

In my program i have a linked list which i want to print all of her elements.
i want to print the elements in a specific order therefore i need first to sort the linked list.
The problem is that i need to sort it according to 3 parameters- I mean after i finish one paramter i move to the next parameter and that`s exactly my problem i don`t know how to
sort according to the second parameter without ruining the sort according to the first parameter.
Thanks for the help,

xpi0t0s 2May2009 02:51

Re: problem implementing- printing sorted linked list
 
In your comparison routine you will need to code how it determines which element comes first.
So if you sort, say, people first by age then by height, this means you first compare two people's ages, then if those are equal you compare the heights. Let's say you're writing a function to determine if person1 > person2:
Code:

if person1.age > person2.age // 1
  return TRUE
else if person1.age==person2.age // 3
{
  if person1.height > person2.height
    return TRUE // 3a
  else
    return FALSE // 3b
}
else return FALSE // 2

1: If person1.age > person2.age then we return TRUE immediately.
2: If person1.age < person2.age then we return FALSE immediately
3: If person1.age == person2.age then we need to look at the height
3a: person1.height > person2.height so we return TRUE
3b: person1.height <= person2.height so we return FALSE.


All times are GMT +5.5. The time now is 09:31.