Thread: Searching a Linked-List

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    Question Searching a Linked-List

    hi , i need help !!!!!!!

    if i have a linked list structure

    typedef struct person{
    char name[MAX];
    char address[MAX];
    struct person *next;
    } List;

    typedef List *ManuPtr;


    how can i search this linked list to print out all person with the same name ????

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    Hyaline,

    It would help if you posted the code you have. Or, if you don't have any code, then pseudocode/logic.

    I'll try to answer your question as is, but (as a disclaimer) I'm horribly tired and my mind is not working right.

    The way I would go about this is to have a function that will:

    Step through the entire list, from the first pointer until you hit the NULL termination.

    -->For each node in the list, compare the target's name with the input value. I would use strcmp(). You probably haven't studied string functions yet, but strcmp is in your book and it's very easy to understand.


    Again, it would help to post code. From what I see, I can't even be sure you have a properly built list.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    while ( list != NULL ) {
      if ( strcmp( list->name, "fred" ) == 0 ) {
        printf( "yo!!\n" );
      }
      list = list->next;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM