Thread: help with adding item to linked list in C

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

    Angry help with adding item to linked list in C

    Code:
    shift(buffer,4); 
    tmp_buffer1 = malloc(strlen(buffer) * sizeof(char)); 
    get_first_num(buffer,tmp_buffer1);
    check = strlen(tmp_buffer1)+1;
    shift(buffer,check); 
    check=0; item_num=0; tmp_item_list=item_list; 
    while(tmp_item_list) {
    	if (strcmp(tmp_item_list->number,tmp_buffer1)==0 && atoi(buffer)>atoi(tmp_item_list->current_price)) { 
    		tmp_buffer2 = malloc(strlen(buffer) * sizeof(char)); strcpy(tmp_buffer2, tmp_item_list->name);
    		tmp_buffer3 = malloc(strlen(buffer) * sizeof(char)); strcpy(tmp_buffer3, tmp_item_list->number);
    		tmp_buffer4 = malloc(strlen(buffer) * sizeof(char)); strcpy(tmp_buffer4, buffer);
    		item_list = add_item(tmp_buffer2, tmp_buffer3, tmp_buffer4, "DUDE", item_list);
    		remove_item(item_list, item_num+1, 0);	
    		send_message(i, "OK"); check=1; break; 
    	}
    	tmp_item_list=tmp_item_list->next; item_num++;
    }
    if (check==0) send_message(i, "NO");
    What i am trying to do is edit the strings of a node in a linked list.
    The way i am doing it is, first finding the node in a copied version of the linked list (which i don't think i did right), then making a new node and coping the details to the new node, and adding my own string, then add the new node to the front of the list, then delete the found node from the list. Right now this isn't working as the list isn't circular, just linear (supposed to be linear and stops) which stops somewhere. After doing this, the list ends up repeated like 2 times... which is bad... what is wrong here?
    Last edited by omega666; 04-04-2011 at 07:41 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it's not circular, all you should have to do is:
    Code:
    for( node = firstnode; node != NULL; node = node->next )
    {
        ...do whatever it is you are trying to do...
    }
    If it is circular, then you just need to keep track of where you started, and if you are back there again, stop looking.


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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    i changed it to
    Code:
    						item *node = NULL;
    						for(node = item_list; node != NULL; node = node->next) {
    							if (strcmp(node->number,tmp_buffer1)==0 && atoi(buffer)>atoi(node->current_price)) { 
    								node->high_bid="dude";
    								check=1;
    								break;
    							}
    						}
    but still a problem, since it didn't make a copy, the original list called item_list is now null after this for loop...


    does this have to be in a function? btw, i have it in my main function...

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    i changed it again to
    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;
    								}
    							}
    						}

    and its still copying a copy of the list to the end, making it repeat twice, i dont see where the pointers are going wrong...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Presently you have two threads running for essentially the same code...

    Please pick one or the other and stick to it.

    Thanks.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    ok delete this thread then, since the other is fine

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The way i am doing it is, first finding the node in a copied version of the linked list (which i don't think i did right),
    I'd do that right before moving on.

    then making a new node and coping the details to the new node, and adding my own string, then add the new node to the front of the list, then delete the found node from the list.
    I can understand wanting to copy a linked list to preserve the original list, but is there a reason you don't simply change the node's data after you find it?

    After you've found the node you want to edit, do this:

    strcpy( foundnode->highbid, "dude" );

    You should be done after that.

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. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM