Thread: Linked List Help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    20

    Linked List Help

    Hello Everyone,

    I'll just jump right into it.

    Code:
    void searchLibrary(book *list)
    {
        string key;
        string search;
        book *searchList = NULL;
        book *temp = NULL;
        book *found = NULL;
        int ctr = 0;
        int i = 0;
        int resp = 0;
        
        screenHeader("Search Function");
    
    
        cout << "                       Type out one of the options below." << endl << endl << endl << endl;
    
    
        cout << "\tHow would you like to search entries?" << endl << endl;
        cout << "\t\t- Author" << endl << endl;
        cout << "\t\t- Title" << endl << endl;
        cout << "\tSearch entires by: ";
        getline(cin, search);
    
    
        if(search.at(0) == 'a' || search.at(0) == 'A'){
            //search by author
            cout << endl << "\tEnter the author: ";
            getline(cin, key); // Key to look for when searching
    
    
            while(list){ // Search the whole array
                if(key == list->author){ // If a match
                    if(ctr == 0){
                        found = new book;
                        found = list;
                        searchList = found;
                        found->next = NULL;
                        ctr++; // Counter for the number of results found
                    }
                    else{
                        temp = new book;
                        temp = list;
                        temp->next = NULL;
                        found->next = temp;
                        found = found->next;
                    }
                }
                list = list->next;
            }
            if(ctr == 0) // If the counter equals zero, then no results were found
                cout << endl << endl << "                           ** No results were found **" << endl << endl << endl;
            else{ // Display the results
                system("cls");
                screenHeader("Search Results");
                cout << "\tResults Found: " << ctr << endl << endl;
                tableDisplay(searchList);
            }
            
            transitionScreen();
        }
        else if(search.at(0) == 't' || search.at(0) == 'T'){
            //search by title
            cout << endl << "\tEnter the title: ";
            getline(cin, key);
    
    
            while(list){ // Search the list
                if(key == list->title){ // If there is a match
                    if(ctr == 0){
                        found = new book;
                        found = list; 
                        searchList = found;
                        found->next = NULL;
                        ctr++; // Counter for the number of results found
                    }
                    else{
                        temp = new book;
                        temp = list;
                        temp->next = NULL;
                        found->next = temp;
                        found = found->next;
                    }
                }
                list = list->next;
            }
            if(ctr == 0) // If the counter equals zero, then no results were found
                cout << "                           ** No results were found **" << endl << endl << endl;
            else{ // Display the results
                system("cls");
                screenHeader("Search Results");
                cout << "\tResults Found: " << ctr << endl << endl;
                tableDisplay(searchList);
            }
            transitionScreen();
        }
        else{
            cout << "You did not enter one of the following options." << endl << endl << endl;
            transitionScreen();
        }
        
    }
    Code:
    searchLibrary(orderList);
    orderList is the head of a list I'm passing to the search function. The problem is in the search function, when found->next = NULL executes, list->next also updates to equal NULL, which means I can no longer continue to search list.

    Thanks for your help!

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    The problem is in the search function, when found->next = NULL executes, list->next also updates to equal NULL, which means I can no longer continue to search list.
    I didn't really look at your code but if you are only doing one assignment, this means that found and list have the same value which is something you don't want. So make sure found and list don't have the same value because the only way that assigning the value of next to null affects another pointer is if those pointers both point to the same thing.

    Just like this :
    Code:
                        found = new book; // you request a heap allocation
                        found = list; // you immediately overwrite the location of the allocation with a previously existing one

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    Ok so I understand what you're saying. How do I make them not point to the same thing? Do I send the address of the original list or a copy of it?
    Thanks!

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Sorry this is a late reply but I'm going to answer your question with another question, what do you want "found" to do? What is it? What's its purpose in the program? Answering these will definitely help you answer the question you asked.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    I'm searching "list" and if there is a match I want "found" to copy of the node from "list". Then I would add "found" to "searchList", which is a list of all matches. Hopefully this answers your question. Also sorry for the late reply

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by calCOOLus View Post
    I'm searching "list" and if there is a match I want "found" to copy of the node from "list". Then I would add "found" to "searchList", which is a list of all matches. Hopefully this answers your question. Also sorry for the late reply
    Here's the beauty, you just answered your own question. You know what you want to do. So do it.

    Your biggest problem is that you're having trouble with pointer semantics and what they really entail.

    If you want your code work, you need to use this instead of what you have now:
    Code:
    if(ctr == 0)
    {
        found = new book;
        *found = *list; // use a memcpy routine, but the idea is, you want what found points at to be the same bytes as what list points to
        searchList.push(found); // better to use basic stack/linked list structure
    }
    Last edited by MutantJohn; 05-17-2015 at 04:07 PM.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    Thank you so much!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  2. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread