Linked List, a pointer problem?

This is a discussion on Linked List, a pointer problem? within the C Programming forums, part of the General Programming Boards category; Hello, I'm new to linked lists and I'm in a bit of a bind. I need to check to see ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Post Linked List, a pointer problem?

    Hello, I'm new to linked lists and I'm in a bit of a bind. I need to check to see if something already exists in the list before adding it. If it does exist, it should not be added. The problem is if you add it the first time, it adds to the list. If you add it a second time it adds to the list, which it shouldn't. If you add it a third time, it will tell you it was not added...I think I need to add an if statement for this specific situation, or change the while parameters...here's what I have...

    Code:
        // Check if car already exists
        if ((*head) == NULL) // Add car to empty list
            ;
        else
        {
            crnt = (*head); // Point to beginning of list
            while(crnt->next != NULL)
            {
                if (strcmp(plate, crnt->plate) == 0) /* plate is what user just entered,
                                                                            crnt->plate is what's already in list */
                {
                    printf("\nCar %s not added.\nThis car already exists!\n", plate);
                    return;
                }
                crnt = crnt->next;
            }
        }
    Help?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Are you really sure the plates are the same?
    No trailing space, or other minor differences which would cause strcmp to fail?

    Do you have a test case to show, say
    Code:
    int main ( ) {
      addlist(list,"abc123");
      addlist(list,"def456");
      addlist(list,"abc123"); // this one should be rejected
    }
    If something like that works, then go back and check your input routines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    This is actually the code I have in the menu...I'll have to try moving it into the function, would it matter?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Not at all.
    It would be better as a function anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 11:10 AM
  2. Linked list probs
    By mouse163 in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 05:41 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 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