我正在尝试实现一些函数来操作链表。实现是一个模板类型名 T,类是“List”,它包括一个“head”指针和一个结构:
struct Node { // the node in a linked list
T* data; // pointer to actual data, operations in T
Node* next; // pointer to a Node
};
由于它是一个模板,并且“T”可以是任何数据,我该如何检查列表的数据以查看它是否与输入到函数中的数据匹配?
该函数被称为“检索”,并接受两个参数,数据和指针:
bool retrieve(T target, T*& ptr); // This is the prototype we need to use for the project
“bool检索:类似于remove,但不从列表中删除。如果列表中有重复项,则检索遇到的第一个。如果返回值为false,则第二个参数不可靠。例如,”
Employee target("duck", "donald");
success = company1.retrieve(target, oneEmployee);
if (success) { cout << "Found in list: " << *oneEmployee << endl; }
该函数的调用如下:
company4.retrieve(emp3, oneEmployee)
因此,当您计算 *oneEmployee 时,您将获得该指针的数据(在本例中,数据是 Employee 类型)。
(此外,这是假设所有数据类型都有适当的重载运算符)
我希望到目前为止这是有道理的,但我的问题是在遍历列表时比较参数中的数据和数据。(我们使用的数据类型都包括等式运算符的重载,所以 oneData == twoData 是有效的)
这是我到目前为止所拥有的:
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 Reading Access 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;
}
这是 Employee 类的实现:
//-------------------------- 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;
}
我将检查 NULL 指针,但我只想让它工作,并将在包含我正在检查的数据的列表上对其进行测试。
感谢任何可以提供帮助的人,并且像往常一样,这是一门课程,所以我不期望或不想要答案,但是任何关于可能出错的提示都会有很大帮助!