我试图编写一个函数来查看链表中是否有更多的节点要搜索,到目前为止我得到的是一个seg错误。关于我需要改变什么的想法?
迭代器课程:
bool Iterator::hasNext(){
Node* temp = current->getNext();
if(temp == NULL){
return(false);
}
else{
return(true);
}
列表类:
void List::addFirst(void* obj)
{
Node* newNode = new Node(obj);
newNode->setNext(head);
head = newNode;
}
节点类:
Node* Node::getNext()
{
return(next);
}
主要课程:
List list1;
for(int i = 0; i < 4; i++) {
list1.addFirst(stars[i]);
}
Iterator itr(&list1);
while(itr.hasNext()) {
std::cout << ((char*)itr.get()->getItem())
<< std::endl;
itr.advance();
}
答案 0 :(得分:-2)
我不确定,但你可以 试试
//directly accessing the next and comparing
return if(current->getNext() == NULL)
直接将其存储在单独的临时节点中。