I'm trying to implement some functions to manipulate a linked list. The implementation is a template typename T and the class is 'List' which includes a 'head' pointer and also a struct:
struct Node { // the node in a linked list
T* data; // pointer to actual data, operations in T
Node* next; // pointer to a Node
};
Since it is a template, and 'T' can be any data, how do I go about checking the data of a list to see if it matches the data input into the function?
The function is called 'retrieve' and takes two parameters, the data and a pointer:
bool retrieve(T target, T*& ptr); // This is the prototype we need to use for the project
"bool retrieve : similar to remove, but not removed from list. If there are duplicates in the list, the first one encountered is retrieved. Second parameter is unreliable if return value is false. E.g., "
Employee target("duck", "donald");
success = company1.retrieve(target, oneEmployee);
if (success) { cout << "Found in list: " << *oneEmployee << endl; }
And the function is called like this:
company4.retrieve(emp3, oneEmployee)
So that when you cout *oneEmployee, you'll get the data of that pointer (in this case the data is of type Employee).
(Also, this is assuming all data types have the apropriate overloaded operators)
I hope this makes sense so far, but my issue is in comparing the data in the parameter and the data while going through the list. (The data types that we use all include overloads for equality operators, so oneData == twoData is valid)
This is what I have so far:
template <typename T>
bool List<T>::retrieve(T target , T*& ptr) {
List<T>::Node* dummyPtr = head; // point dummy pointer to what the list's head points to
for(;;) {
if (*dummyPtr->data == target) { // EDIT: it now compiles, but it breaks here and I get an Access Violation error.
ptr = dummyPtr->data; // set the parameter pointer to the dummy pointer
return true; // return true
} else {
dummyPtr = dummyPtr->next; // else, move to the next data node
}
}
return false;
}
Here is the implementation for the Employee class:
//-------------------------- constructor -----------------------------------
Employee::Employee(string last, string first, int id, int sal) {
idNumber = (id >= 0 && id <= MAXID? id : -1);
salary = (sal >= 0 ? sal : -1);
lastName = last;
firstName = first;
}
//-------------------------- destructor ------------------------------------
// Needed so that memory for strings is properly deallocated
Employee::~Employee() { }
//---------------------- copy constructor -----------------------------------
Employee::Employee(const Employee& E) {
lastName = E.lastName;
firstName = E.firstName;
idNumber = E.idNumber;
salary = E.salary;
}
//-------------------------- operator= ---------------------------------------
Employee& Employee::operator=(const Employee& E) {
if (&E != this) {
idNumber = E.idNumber;
salary = E.salary;
lastName = E.lastName;
firstName = E.firstName;
}
return *this;
}
//----------------------------- setData ------------------------------------
// set data from file
bool Employee::setData(ifstream& inFile) {
inFile >> lastName >> firstName >> idNumber >> salary;
return idNumber >= 0 && idNumber <= MAXID && salary >= 0;
}
//------------------------------- < ----------------------------------------
// < defined by value of name
bool Employee::operator<(const Employee& E) const {
return lastName < E.lastName ||
(lastName == E.lastName && firstName < E.firstName);
}
//------------------------------- <= ----------------------------------------
// < defined by value of inamedNumber
bool Employee::operator<=(const Employee& E) const {
return *this < E || *this == E;
}
//------------------------------- > ----------------------------------------
// > defined by value of name
bool Employee::operator>(const Employee& E) const {
return lastName > E.lastName ||
(lastName == E.lastName && firstName > E.firstName);
}
//------------------------------- >= ----------------------------------------
// < defined by value of name
bool Employee::operator>=(const Employee& E) const {
return *this > E || *this == E;
}
//----------------- operator == (equality) ----------------
// if name of calling and passed object are equal,
// return true, otherwise false
//
bool Employee::operator==(const Employee& E) const {
return lastName == E.lastName && firstName == E.firstName;
}
//----------------- operator != (inequality) ----------------
// return opposite value of operator==
bool Employee::operator!=(const Employee& E) const {
return !(*this == E);
}
//------------------------------- << ---------------------------------------
// display Employee object
ostream& operator<<(ostream& output, const Employee& E) {
output << setw(4) << E.idNumber << setw(7) << E.salary << " "
<< E.lastName << " " << E.firstName << endl;
return output;
}
I will include a check for NULL pointer but I just want to get this working and will test it on a list that includes the data I am checking.
Thanks to whoever can help and as usual, this is for a course so I don't expect or want the answer, but any tips as to what might be going wrong will help immensely!