Thread: How to search a linked list for data and return the data.

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    11

    Question How to search a linked list for data and return the data.

    I know I am not supposed to post my homework on here but I asked my teacher for help but she won't answer me. For this program I am not allowed to use vectors or std::

    I have a function called findStudent that takes a linked list structure as an argument. The function asks the user for a student's id number. I build a temporary structure object and search the linked list by calling the "retrieve" function. If the student is found them I am supposed to display the student's entire record. However, I can't get my function to find the student.

    I use a structure named Student that stores all the student's info, and a linked list where each student's info is a node. In my structure I have a data member
    Code:
    int id
    . In my linked list program, I call the function "retrieve" that is supposed to return the node's data I am searching for using the parameter.

    This is my findStudent function:
    Code:
    void findStudent(const LinkedList<Student> &list)
    {
        int id;        
        
        cout << "Please enter student ID number: ";
        cin >> id;
    
        Student temp{ id };                     //temporary struct object
    
        if (list.retrieve(temp) == false)    //can't find the student id i am searching for
            cout << "Student not found.\n";
        else
            cout << temp;
    }
    And this is the retrieve function I call above:
    Code:
    template <class TYPE>
    bool LinkedList<TYPE>::retrieve(TYPE &dataIn) const
    {
        bool success = false;
        Node<TYPE> *pTemp = front;
    
        while (pTemp != nullptr && pTemp->data < dataIn)            
        {
            pTemp = pTemp->next;
        }
        if (pTemp != nullptr && pTemp->data == dataIn)
        {
            success = true;
            dataIn = pTemp->data;
        }
    
        return success;
    }
    How can I get retrieve to find the student I am searching for?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,084
    Your linked list template's retrieve member function should have a const reference parameter instead:
    Code:
    template <class TYPE>
    bool LinkedList<TYPE>::retrieve(const TYPE &dataIn) const
    The reason is that the act of finding if dataIn matches an element in the linked list conceptually does not change dataIn, so there's no reason for it to be a non-const reference parameter. What you're doing now with this:
    Code:
    dataIn = pTemp->data;
    is conceptually useless because dataIn is already equal to pTemp->data. If what you want to do is actually "retrieve" the element as the name implies, then instead of returning a bool, you could have your function return an iterator (or a pointer) to the Student object, returning an end() iterator (or null pointer) if the Student object is not found.

    Next, what you need to decide is how a generic "retrieve" member function should work to decide whether an element is or is not a match for the argument. What you're doing right now is assuming that objects of type TYPE can be compared by using operator< and operator==. This can work, but it means that you need to overload those operators for Student. Frankly, you don't need operator< unless the linked list is sorted, and if you're assuming that the linked list is sorted, there should be some indication that this is the case. So what I suggest is that you simplify your retrieve function to only compare by operator==, then overload operator== for Student to compare by id.
    Last edited by laserlight; 09-21-2020 at 04:52 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    11

    Question

    Ok so I updated my findStudent code to:
    Code:
    void findStudent(const LinkedList<Student> &list)
    {
        int id;        
        
        cout << "Please enter student ID number: ";
        cin >> id;
    
    
        Student temp{ id };
        list.retrieve(temp);
        cout << temp;
    }
    And now my retrieve function looks like:
    Code:
    template <class TYPE>
    TYPE LinkedList<TYPE>::retrieve(TYPE &dataIn) const
    {
        Node<TYPE> *pTemp = front;
    
    
        while (pTemp != nullptr && pTemp->data < dataIn)            
        {
            pTemp = pTemp->next;
        }
        if (pTemp != nullptr && pTemp->data == dataIn)
        {
            dataIn = pTemp->data;
        }
    
    
        return dataIn;
    }
    I tried to make the parameter TYPE &dataIn a constant but my program would not work. But now I have another problem. Instead of printing out the student's id, name, gpa, and major it only prints out the id. This info is stored in the structure Student as:
    Code:
    struct Student
    {
        int id;
        char name[50];        
        float gpa;
        char major[6];
         //overloaded operator functions
    }
    How can I get it to display the student's entire record?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,084
    Quote Originally Posted by meagangramlin
    I tried to make the parameter TYPE &dataIn a constant but my program would not work.
    That's because you're using dataIn as an input/output parameter. I was trying to get you to redesign your program to use it as an input parameter, then provide the output by another way.

    Quote Originally Posted by meagangramlin
    But now I have another problem. Instead of printing out the student's id, name, gpa, and major it only prints out the id.
    That's because now you're still using dataIn as an input/output parameter, except that you didn't actually assign to it in the function template.

    Given what it looks like you want to do, i.e., assign to an output parameter, the way I would design this is to have retrieve be a function template itself that takes a predicate and has a TYPE& parameter as an output parameter, returning a bool to indicate found or not found:
    Code:
    template<typename T>
    template<typename Pred>
    bool LinkedList<T>::retrieve(T& result, Pred predicate) const
    {
        Node<T>* node = front;
        while (node)
        {
            if (predicate(node->data))
            {
                result = node->data;
                return true;
            }
            node = node->next;
        }
        return false;
    }
    You would then use it like this:
    Code:
    int id;
    // ... read into id ...
    
    Student result;
    if (students.retrieve(result, [id](const Student& s) { return id == s.id; }))
    {
        cout << result;
    }
    else
    {
        cout << "Student not found.\n";
    }
    Of course, this may be beyond the scope of your exercise. If so, what I suggest for you to just get this working though is to go back to your post #1, revert the code to that state, and then just overload operator== to compare Student ids. I also suggest renaming dataIn to something that reflects the fact that it is an input/output parameter, not an input parameter.
    Last edited by laserlight; 09-21-2020 at 06:56 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list data structure
    By Player777 in forum C Programming
    Replies: 3
    Last Post: 12-23-2019, 11:55 PM
  2. Search the data in the linked list
    By akif13 in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2014, 12:38 PM
  3. linked list data input.
    By Springy in forum C Programming
    Replies: 5
    Last Post: 10-19-2009, 01:55 AM
  4. linked list/ stored data
    By SpEkTrE in forum C++ Programming
    Replies: 12
    Last Post: 04-11-2005, 02:51 PM
  5. Read Data-Linked list pro
    By Supra in forum C Programming
    Replies: 8
    Last Post: 09-15-2001, 05:34 PM

Tags for this Thread