Thread: Insert a Node In a Linked List using 2 or more sort criteria ?

  1. #1
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18

    Insert a Node In a Linked List using 2 or more sort criteria ?

    Hi all...

    I know how to insert a node into a linked list using 1 criterion

    But if, for example, i have to insert a node using 2 criteria like, e.g., surname and name, so the list will be already sorted, how have I to do ?

    E.g. of my struct

    Code:
    struct person {
    
    char *name
    char *surname
    
    struct person *nextPtr
    
    }
    sorry for my bad english

    Thx in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for each node
        if this node's next pointer comes after what you are to insert
            make the insert -> next point to this -> next
            make this -> next point to insert
            have a beer in celebration of a job well done

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    with one criteria you would prbably use something like strcmp(sirname) to find out where to insert the new node. Using two or more criteria is just an extension of that idea
    Code:
    if( strcmp(node1->surname, node2->surname) == 0)
    {
      last names are the same, so now try first names
       if( strcmp( node1->name,node2->name) == 0)
      {
            // make other tests here if you need to check other criteria
            // like people's sex, age, address, etc.
            return 0; // both the same too.
      }
    }

  4. #4
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Thx Ancient..
    Last edited by delphi; 09-10-2005 at 11:06 AM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A struct needs a semicolon on the end . . . and for strcmp() you should #include <string.h>
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  2. Linked list probs
    By mouse163 in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 05:41 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM