deleting for a linked list

This is a discussion on deleting for a linked list within the C Programming forums, part of the General Programming Boards category; for some reason every time i try to delete the plate from the linked list, it returns a zero value. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    71

    Unhappy deleting for a linked list

    for some reason every time i try to delete the plate from the linked list, it returns a zero value. i know that
    Code:
     while(crnt->next !=NULL && crnt->next->license != license)
        {
            crnt = crnt->next;
            
        }
    is where i am having my problem. it isnt searching for the value and is hitting null to return 0. what am i doing wrong?
    Code:
    
    int removecar(struct cars** head, char* license)
    {
    
     struct cars* temp;
     struct cars* crnt;
     
       
       if((*head) == NULL)
            return 0; 
        
        if((*head)->license == license)
        {
            temp = *head;
            (*head) = (*head)->next;
            free(temp);
            return 1;
        }
        crnt = *head;
        while(crnt->next !=NULL && crnt->next->license != license)
        {
            crnt = crnt->next;
            
        }
        if(crnt->next==NULL) //end of list reach and plate not found
        return 0;
        
        temp = crnt->next;
        crnt->next = crnt->next->next;
        free(temp);
        
        return 1;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    to compare strings you should use strcmp
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i dont know which to compare.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i tried changing that line to
    Code:
    while(strcmp(license, crnt->next)==0)
    but now its automatically deleting the second character no matter what it is.
    as u cant see im not too familiar with strcmp.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you didn't bother looking it up because why?

    But in any event strcmp returns 0 when the two are the same -- so if you want to keep going while they're different you should use !=. And you need to compare license with crnt->next->license -- your compiler should complain about you trying to compare a string with a struct.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i did look it up and i found the same examples that did nothing for me. the compiler did complain but didnt stop me from running the program. in my mind im thinking if crnt = *head and then i need to loop until crnt finds license. when they match delete crnt. what is the difference between me using !=0 and >0?

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    Code:
    
        while(strcmp(license, crnt->next->license)!=0){
    
            crnt = crnt->next;
        }

    my program crashes when i try to delete still

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You still need the crnt->next != NULL check.

    (Edit: I see now the special case at the beginning. Don't forget to change that to strcmp too.)
    Last edited by tabstop; 09-14-2008 at 01:12 AM.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    Code:
     if((*head) == NULL)
            return 0; 
        
        if((*head)->license == license)
        {
            temp = *head;
            (*head) = (*head)->next;
            free(temp);
            return 1;
        }
    this is frees *head if it is the one i am looking for. do i need a strcmp for that also?

    Code:
    if(crnt->next==NULL) //end of list reach and plate not found
        return 0;
    is if the crnt->next is null.

    i dont have a dummy node

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by vidioholic View Post
    Code:
    if(crnt->next==NULL) //end of list reach and plate not found
        return 0;
    Yes, but if you don't have the check in your while loop you're not going to actually ever stop but run off the end of your list.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok but why is it deleting the second value in the list every time instead of the one i want?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have
    Code:
       while(crnt->next != NULL && strcmp(license, crnt->next->license)!=0)
    then it isn't. Show new code.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i had:
    Code:
        while(crnt->next != NULL && strcmp(license, crnt->next->license)!=0){
    
            crnt = crnt->next;
        
        
        if(crnt->next==NULL) //end of list reach and plate not found
        return 0;

    then i changed it to what you said. now its not deleting anything... do you want me to post me entire code? maybe there is an issue somewhere else in my code?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Where did your closing curly brace go? Yes, show the whole thing then.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    Code:
    int removecar(struct cars** head, char* license)
    {
    
     struct cars* temp;
     struct cars* crnt;
     
       
       if((*head) == NULL)
            return 0; 
        
        if(strcmp((*head)->license, license)==0)
        {
            temp = *head;
            (*head) = (*head)->next;
            free(temp);
            return 1;
        }
        crnt = *head;
        
    
        while(crnt->next != NULL && strcmp(license, crnt->next->license)!=0)
        {
            crnt = crnt->next;    
            return 0;
        }
    
        temp = crnt->next;
        crnt->next = crnt->next->next;
        free(temp);
        
        return 1;
    }
    ok im able to delete only 3 lines out. lets say i add: a b c d e.... i will only be able to delete a b or c. anything after that, it will return a 0

    also if i delete something and then add another, it is adding it after the head. it is putting it into the second spot instead. lol idk why its doing that
    Last edited by vidioholic; 09-14-2008 at 12:00 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. deleting a linked list
    By gordy in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 05:01 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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