链表操作,检索数据 c++ 的问题



我正在尝试实现一些函数来操作链表。实现是一个模板类型名 T,类是"List",它包含一个"头"指针和一个结构体:

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 检索:类似于删除,但不从列表中删除。如果列表中存在重复项,则检索遇到的第一个重复项。如果返回值为 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 指针的检查,但我只想让它工作,并将在包含我正在检查的数据的列表上对其进行测试。

感谢任何可以提供帮助的人,像往常一样,这是一门课程,所以我不期望也不想要答案,但任何关于可能出错的提示都会有很大帮助!

在未编译的行中,您正在将Node结构(左侧)与类型为 T 的对象(右侧)进行比较。 相反,您希望比较两个类型 T 的对象。 所以你需要这样做(假设,正如你所说,类型T指定了一个operator==()方法):

if (*dummyPtr->data == target) {

最新更新