searching linked lists

This is a discussion on searching linked lists within the C Programming forums, part of the General Programming Boards category; Need help searching a linked list. This is what I have so far but I am not sure if the ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    searching linked lists

    Need help searching a linked list. This is what I have so far but I am not sure if the while loop is correct. I also am unsure if the code is completely correct. Any help would be appreciated.


    //structure for writing to and reading from file**
    struct filebook
    {
    int code;
    char title[20];
    char author[20];
    int pages;
    }ebook;

    //************************************************

    //structure for creating the linked list**********
    struct book
    {
    int code;
    char title[20];
    char author[20];
    int pages;
    struct book*next;
    }*head,*work,*node,*previous,*found,libry;

    void search(void)
    {
    system("cls");
    printf ("\t\t\tTusket Municipal Library\n\n\n");
    printf("Which book code do you want to search for?: ");
    scanf("%d",&libry.code);
    printf("\n");

    if(libry.code=&libry.code)
    {
    found=libry.code;
    }

    while(found != NULL && found->next!= found) //DO NOT reverse condition
    {
    previous=found;
    found=found->next;
    }
    if (found == NULL)
    {
    printf("Sorry, not in the list.");
    printf("\n");
    getch();
    main_menu();
    }
    else
    {
    printf("Your book has been found in the list.");
    printf("\n");
    getch();
    main_menu();
    }
    }

    We are searching by code.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if(libry.code=&libry.code)
    I believe you want == here, not =. Also, you're testing the address of libry.code with the contents of libry.code, what is this test supposed to accomplish?

    >found=libry.code;
    The found variable is a pointer to a struct book, libry.code is an int. This is just a little off. Perhaps a key variable that is defined as an int to hold the user's choice.

    >if (found == NULL)
    Since you always walk to the end of this list this will be true every time. Declare an explicit iterator for the list and if you find the key then point found to where the iterator is. Or declare the function to return a struct book * and return the iterator.

    >found != NULL && found->next!= found
    This test confuses me a bit. What are you testing for with found->next != found?

    This code is a bit nasty and the goal could be accomplished more easily if you gave more responsibility to the calling function such as checking if the item was found by seeing if a returned value is NULL:
    Code:
    struct book *search(struct book *head)
    {
      int key;
      struct book *iter;
    
      printf("\t\t\tTusket Municipal Library\n\n\n");
      printf("Which book code do you want to search for?: ");
      scanf("%d",&key);
    
      for(iter=head; iter!=NULL; iter=iter->next)
        if(iter->code == key) return iter;
      return NULL;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  2. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  4. Searching Double Linked lists
    By AmazingRando in forum C Programming
    Replies: 5
    Last Post: 09-23-2003, 12:15 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM

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