I've been able to find Q&A's for inserting and deleting objects from a linked list. But my problem is accessing and updating those objects from a linked list.
The following code is part of a school project wherein I need to make a Linked List of Payroll objects. I need to be able to insert, delete, search by specific parameters, and update employee payroll info. I'm having no problems inserting and deleting. But I'm somewhat lost on how to search and access those objects to interact with their variables.
In the InList function, I pass a linked list and an int, create the Payroll object and assign the int as the employee number variable. Then I use P's search function and pass that payroll object as the argument.
void InList(const orderedLinkedList<Payroll>& P, int employee_number)
{
Payroll payListEmpNum;
payListEmpNum.setEmployeeNumber(employee_number);
if (P.search(payListEmpNum) == 1)
{
payListEmpNum.printPayroll();//this is just printing my local employee_number.
}
else
{
cout << "Sorry, that employee was not found.\n" << endl;
};
}
This is the search function for my orderedlinkedlist class. It iterates through the list, and tests each object's employee number against the object that I sent. I can send my current pointer to my Payroll class to print the records, but that doesn't give me access to the data.
template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const
{
bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first; //start the search at the first node
while (current != NULL && !found)
if (current->info >= searchItem)
found = true;
else
current = current->link;
if (found) {
found = (current->info == searchItem); //test for equality
}
return found;
}//end search
However, since the search function doesn't return any data, InList only prints my local variable, employee_number, and null for all the other variables.
I'm not sure how to get access to my object's variables. Should I write a different function to handle this? Is this a pointer problem?
Thanks!
searchshouldn't return a bool but rather the pointer to the first element found, if it was found, and a pointer to null, if none were found.template <class T> const T *find( const orderedLinkedList<T>& list, std::function<bool(const T&)> predicate )looks nice. If your list is ordered by some key, you cannot modify the key without having to re-order. For other entries, of course you could change. Then a non-const version of the above function would work.