C++ 如何从列表中删除节点

C++ 如何从列表中删除节点,c++,class,linked-list,C++,Class,Linked List,我希望我的程序删除节点并返回RemoveHead()函数中的项。我不确定是什么错误。在main()函数中,一切正常,但我的程序没有删除Head,它仍在打印上一个列表L1:一个非空列表 为了更清楚,我正在上传我的输出和期望的输出 这是我的节目: int main() { cout << "===== Testing Step-1 =====\n"; cout << "Testing default constructor...\n"; LinkedL

我希望我的程序删除节点并返回
RemoveHead()
函数中的项。我不确定是什么错误。在
main()
函数中,一切正常,但我的程序没有删除
Head
,它仍在打印上一个列表
L1
:一个非空列表

为了更清楚,我正在上传我的输出和期望的输出

这是我的节目:

int main()
{
    cout << "===== Testing Step-1 =====\n";
    cout << "Testing default constructor...\n";
    LinkedList L1;
    L1.Print(); // should be empty
    cout<<"\nTesting AddHead()...\n";
    for(int i=0; i<10; i++){
        cout << i << ' ';
        L1.AddHead(i);
    }
    cout << endl;
    L1.Print();
    cout << "\nTesting IsEmpty() and RemoveHead()...\n";
    while(!L1.IsEmpty())
        cout << L1.RemoveHead()<< ' '; // should be printed in reverse
    cout << endl; 
    L1.Print(); // should be empty
}

int LinkedList::RemoveHead()
{
    if(Head==NULL)
    {
        cerr << "Error Occured. " << endl;
        exit(1);
    }
    else
    {       
        NodePtr temp;
        temp->Item = Head->Item;
        temp = Head;
        Head = Head->Next;
        delete temp;
    }
 //return 0; to be removed while compilation

bool LinkedList::IsEmpty()
{
  Head==NULL;
  return true;
}
void LinkedList::Print()
{

    if (Head==0)
    {
        cout << "Empty error ." ;
    }
    else
    {
        NodePtr crnt;
        crnt = Head;
        while(crnt!= NULL)
        {
            cout << crnt->Item << " ";
            crnt = crnt->Next;
        }
        cout << endl;
    }
}   

如以下人士在评论中所述:

您的
IsEmpty()
方法应根据以下内容进行重构:

bool LinkedList::IsEmpty()
{
  Head==NULL;
  return true;
}
为此:

bool LinkedList::IsEmpty()
{
  return Head==nullptr;
}

感谢您指出使用。

请提供最低限度的示例代码。目前有很多不鼓励使用的垃圾行。在
IsEmpty()
中,
Head==NULL
您忘记了
if
。只需将其设置为
returnhead==NULL另外,
RemoveHead()
不完整。RemoveHead()有什么问题?@muzzi 1。它缺少一个结束符
}
和2。它不返回值。
bool LinkedList::IsEmpty()
{
  return Head==nullptr;
}