Thread: Help on Linked List Search

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    33

    Help on Linked List Search

    Hello guys..

    i'm stuck (on this problem) while doing revision questions for exam next week. I just understand this question.

    Can you please make it understandable and guidance (on how to do it) if possible??

    5 a) Write a C++ function called searchString that is passed as a null terminated string called inString and a link-list of strings called head. This function searches and returns a boolean value true if inString exists in the linked structure. Otherwise a boolean value false is returned. Assume that each linked list node has the following structure:

    Code:
    struct Node 
    { 
      char directive[10]; 
      Node *next; 
      };
         typedef Node * LNode;   
    		
    bool searchString(LNode &head, char *inString)


    do i have to initialize a link list for this??

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    hopefully this makes things a little clearer.
    Code:
    struct Node 
    { 
      Node(Node* nextNode=0):
        next(nextNode)
      {
      }
      char directive[10]; 
      Node *next; 
    };
    
    void traverseList(Node& node)
    {
      if(node.next)
      {
        traverseList(*node.next);
      }
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      Node n3, n2(&n3), n1(&n2), n0(&n1);
      traverseList(n0); 
      
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    thanks sir..

    what is a null terminated string and how does it work with the linked list?? (just so that i can understand the question even more)

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    when you type in a string literal, e.g. "abcd", the compiler interprets this as an array of 5 chars. [97,98,99,100,0] (97 - 100 are the ascii codes for a-d, respectively) the 0 on the end is the "null character", that tells various string-processing functions that a char* string has ended.

    in your problem, i assume that directive is a string of at most 9 printing characters (with a terminating null byte on the end)

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    still not sure in this

    just don't know where to start.. can't find any help from www also..

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    i showed one possible way to traverse the list. do you understand how to compare strings?

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    no..

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    there are standard functions to do just that, but it really isn't that complicated.

    a string is an array of characters.

    if each value in two arrays are equal, then the two arrays contain the same word (or phrase), and so the strings are equal to each other.

    at this point, why don't you go ahead and do some coding and post your attempt (make sure it at least compiles).

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sivapc View Post
    no..
    Let me google that for you
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  2. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM