how to check if string exists in a struct linked list?

This is a discussion on how to check if string exists in a struct linked list? within the C Programming forums, part of the General Programming Boards category; can someone provide an example of a struct linked list, where it has strings as its values, and then how ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    29

    Question how to check if string exists in a struct linked list?

    can someone provide an example of a struct linked list, where it has strings as its values, and then how do I check if a specific string (say called buffer) exists in the list of structs?

    i dont understand how to make a copy of it to check with
    this is what i have
    Code:
                            
                            // CHECK IF ITEM EXISTS IN THE ITEM LIST
                            check=0; tmp_item_list=item_list;
                            while(tmp_item_list) {
                                if (strcmp(tmp_item_list->number,buffer)==0) {
                                    tmp_buffer0[0]='\0';
                                    strcat(tmp_buffer0,tmp_item_list->number);
                                    strcat(tmp_buffer0," ");
                                    strcat(tmp_buffer0,tmp_item_list->name);
                                    strcat(tmp_buffer0," ");
                                    strcat(tmp_buffer0,tmp_item_list->current_price);
                                    strcat(tmp_buffer0,"\n");
                                    send_message(i, tmp_buffer0); check=1; break;
                                }
                                tmp_item_list=tmp_item_list->next;
                            }
    i want to make a copy because if i dont then
    tmp_item_list=tmp_item_list->next;
    will screw it up

    also this wont work, because somehow when i iterate through the list, it seems the list repeats again...

    this compiles fine with no errors/warnings

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like your list is circular, which of course would mean that if everything else were working right, that it would compile without warnings or errors, because having a circular list isn't a compile time error.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    can someone provide an example of a struct linked list, where it has strings as its values, and then how do I check if a specific string (say called buffer) exists in the list of structs?
    You can discover the word "buffer" in one of your string variables by cycling through your list and using strcmp() or stricmp() to search.

    If you posted the struct definition it would help us enormously....


    You cannot however discover, in run time, if a variable named buffer exists in the struct... you have to have a copy of the struct to know that and the compiler needs to know it at compile time...

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    Code:
    typedef struct item_list {
    	char *name;
    	char *number;
    	char *current_price;
    	char *high_bid;
    	struct item_list *next;
    } item;
    Code:
    check=0;
    item *ptr = NULL;
    for(ptr = item_list; ptr != NULL; ptr = ptr->next) {
    	if (strcmp(tmp_buffer1, ptr->number)==0) { 
    		if (atoi(buffer)>atoi(ptr->current_price)) { 
    			char *tmp_buffer = malloc(strlen(buffer) * sizeof(char)); strcpy(tmp_buffer, buffer);
    			ptr->current_price=tmp_buffer;
    			ptr->high_bid="dude";
    			send_message(i, "OK");
    			check=1;
    		}
    	}
    }

    this is my new problem, i want to edit something when i find the right node, but after this the edit works, but the list is now repeated twice and combined, i think somewhere my pointers is messing up here...

    btw this code isnt a function, its local to the other code.

    can you confirm if this part is ok?
    Last edited by omega666; 04-04-2011 at 10:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 10:34 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-03-2008, 12:10 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. linked list string problem
    By Korda in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:47 PM

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