我有由具有以下结构的节点组成的单链表:
struct date
{
int day, month, year;
};
struct Node
{
string item;
date Exp;
int count;
Node *link;
};
typedef Node* NodePtr;
当我搜索到期日期时,我搜索时会显示所有其他节点,但不会显示第一个节点。当我也更改节点的顺序时会发生这种情况。这是一个简单的错误吗?
这是我用于搜索节点的函数:
NodePtr search_date(NodePtr head, int month, int day, int year)
{
// Point to the head node
NodePtr here = head;
// If the list is empty nothing to search
if (here == NULL)
return NULL;
// Search for the item
else{
//while you have still items and you haven't found the target yet
while (here-> Exp.day != day &&
here-> Exp.month != month &&
here->Exp.year != year &&
here->link != NULL)
here = here->link;
// Found the target, return the pointer at that location
if (here-> Exp.month == month &&
here-> Exp.day == day &&
here-> Exp.year == year)
return here;
// Search unsuccessful, return Null
else
return NULL;
}
}