Thread: linked list search

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    linked list search

    how do i conduct a search in linked list to return all strings starting with the letter provided.. eg search('a',p) should return all words staring with a in the list p

    Code:
    int search(char check,Node *p) {
    while( p!= NULL ) {
        if(p->word == check) {
               printf("%s",p->word);
                        }
    	p = p->next;
            }
         return 0 ;                 
    }
    i got this but its wrong..how do i fix it ??

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> p->word

    What is Node::word?

    >> i got this but its wrong

    Yes, it is. But why? Could it be that char* == char doesn't make any sense?

    >> int search
    >> return 0 ;

    Why return anything, if the value isn't even going to be returned properly?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    yea..i know..but how do i search for the first char ? and then return the corresponding word ?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> yea..i know

    If you know then please show the updated draft.

    >> but how do i

    You can start by asking *specific* questions.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Code:
    int search(char* check,Node *p) {
    while( p!= NULL ) {
        if(strcmp(p->word,check)==0) {
               printf("&#37;s",p->word);
                        }
    	p = p->next;
            }
         return 0 ;                 
    }
    still doesnt work!..i know its wrong though..stcmp compares string..but i need to comapare a char to a string ?..how do i do that ?
    Last edited by rocketman03; 11-22-2008 at 04:17 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Convert the char into a string and use strcmp( ), or just compare the char with the first character in the string and check that the string has a length of 1.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM