problem implementing- printing sorted linked list

Discussion in 'C' started by sucre, May 1, 2009.

  1. sucre

    sucre New Member

    Joined:
    May 1, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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,
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice