Thread: Searching for word in Linked list. example?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    27

    Searching for word in Linked list. example?

    can anyone point to and example showing how to search for words in linked lists? Like somebody inputs a name and then a function will go thru and return it if its there and doing something else if not.

    Ive been googling and usually its just shows how to insert or delete from a list.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Go through the list from head to tail, comparing the element you wish to find using strcmp, stop going through the list at that point.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    thankyou, could you do a little snippet of pseudo code or code to demonstrate?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Nope. There are too many way of implementing a linked list. Why don't you post your attempt if it doesn't work? We expect you to actually put in some sort of effort here. We're not a homework factory.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    27
    does this look viable? not sure where the strcmp would come into play tho

    Code:
    artistNodePtr FindArtist( artistNodePtr *sPtr, char *artistID); {
    while (sPtr != NULL){
        if (sPtr ->data == artistID){
            return sptr
    }
        else{
            sPtr = sPtr -> nextArtist_p
            }
    }
    }
    Last edited by Chucker; 04-08-2012 at 06:53 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Chucker View Post
    can anyone point to and example showing how to search for words in linked lists? Like somebody inputs a name and then a function will go thru and return it if its there and doing something else if not.

    Ive been googling and usually its just shows how to insert or delete from a list.
    Sometimes you just have to go with what you can find. Searching a list is not terribly different from inserting into a list. The steps up to insertion can be exactly searching. (Indeed, a lot of linked list presentations forced sorted order on the list.) Naturally this is pretty dumb in the general case. But if you think about it, finding the place for a new node in a sorted list is exactly like searching for a particular node.

    Just because people don't show you exactly what to do, don't give up on what you found. A lot of programming these days is just figuring out what you need to glue together and what you've done before can be a big hint.

    edit: And yes, remember to call strcmp to compare strings.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching Linked List
    By JamesD in forum C Programming
    Replies: 7
    Last Post: 03-30-2011, 03:34 PM
  2. Searching/counting in a linked list
    By thoseion in forum C Programming
    Replies: 1
    Last Post: 11-11-2006, 08:21 PM
  3. searching a linked list
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-22-2003, 05:08 PM
  4. Searching Linked List
    By csmatheng in forum C Programming
    Replies: 1
    Last Post: 04-25-2002, 03:45 PM
  5. Searching a Linked-List
    By hyaline in forum C Programming
    Replies: 2
    Last Post: 09-12-2001, 12:34 PM