在本实验中,您需要创建AnyList类的成员函数。下面您可以找到类的接口以及Node类的定义。
class Node
{
public:
Node() : data(0), ptrToNext(nullptr) {}
Node(int theData, Node *newPtrToNext)
: data(theData), ptrToNext(newPtrToNext){}
Node* getPtrToNext() const { return ptrToNext; }
int getData( ) const { return data; }
void setData(int theData) { data = theData; }
void setPtrToNext(Node *newPtrToNext)
{ ptrToNext = newPtrToNext; }
~Node(){}
private:
int data;
Node *ptrToNext; // Pointer that points to next node.
};
class AnyList
{
public:
AnyList() : ptrToFirst(nullptr), count(0) {}
// Other member functions of the class...
private:
// Pointer to point to the first node in the list.
Node *ptrToFirst;
// Variable to store the number of nodes in the list.
int count;
初始化一个空列表,ptrToFirst
为nullptr
。所以一个合理的实现应该是检查这个等式:
bool isEmpty() const {
return ptrToFirst == nullptr;
}
或者,您可以检查count
是否为0
:
bool isEmpty() const {
return count == 0;
}