linked list using strstr

This is a discussion on linked list using strstr within the C Programming forums, part of the General Programming Boards category; hi i have created a linked list with nodes and what not. i am trying to now search the nodes, ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    linked list using strstr

    hi i have created a linked list with nodes and what not. i am trying to now search the nodes, expecially the task of each node for a certain string which the user would put in. i have written the code but it doesnt appear to work. i have located where it stops and have indicated it below. any help or tips would be appreciated. thanks
    insert
    Code:
     printf("Search Text: ");
         char ch1[MAX_LINE];                //create array
         fgets( ch1, MAX_LINE, stdin );  // store input into array
    
    	  node *ptr;                   // create pointer
    	                                       
    	  ptr = list;                       //assign to head of linked list
    	  
    	  while(ptr != NULL){       //run through the list
    
    		  char *found;     //create pointer 
    		  found = strstr(ptr->task,ch1);    // assign found to    whatever strstr find. either NULL or something strstr foind
    
    		  if(found != NULL){                  
    		  	printCurrent(ptr);         //if strstr found somethin, print the node
    		  } 
    
    	  ptr = ptr->next;              //go to next node
    	  }
    
    	 printf("\n");

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    > i have located where it stops and have indicated it below
    where?
    probably you need to post the routine where you are creating the list as well..
    In the middle of difficulty, lies opportunity

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    lol sorry...yeah it never goes into
    Code:
    if(found != NULL){                  
        printCurrent(ptr);         //if strstr found somethin, print the node
    }
    mainly because found is always found to be NULL, even when i make such that ptr->task and ch1 are identicle.

    also, as for how the list is created, its a todo list, so everytime an entry is added, a new node is added into the linked list.

    structure for each node is
    Code:
    struct node {
      char *task;
      int   class;
      node *next;
    };
    hope this is what your looking for. the whole code is a massive chunk. thanks

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    strstr returns NULL if the test for the existence of the substring has failed. But, since u say its failing for all inputs, probably there is something wrong with (ptr->task) itself.
    That's why I asked you to post thje routine where you are "creating" the list and not just declaring it.
    traverse the entire list once just to make sure the list is what you have in mind.
    In the middle of difficulty, lies opportunity

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 09:04 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21