删除节点无效

Delete node wont work

本文关键字:无效 节点 删除      更新时间:2023-10-16

我试图删除链表上的节点,但在删除命令之后,我试图在节点内显示数据,我仍然可以显示据称已删除的数据。搜索是我试图删除的节点

int position=0;
  while(admintemp !=NULL)
    {
        position=position+1;
        if(admintemp==search)
        {
            cout<<"found"<<position;
            getch();
            break;
        }   
        admintemp = admintemp->next;    
    }

    node *body = new node;
    node *admintemp = new node;
    if(position>0)
    {
        admintemp = adminhead;
        for (int i= 1;i<position;i++)
        {
            body = admintemp;
            admintemp = admintemp->next;
        }
        body->next=admintemp->next;
        cout<<"deleting";
        getch();
        delete admintemp;
    }    

通过delete d指针读取数据是"未定义行为" -这意味着编译器可以做任何它喜欢的事情,你的程序没有意义。

可能得到你期望的结果,你可能得到崩溃,你可能得到从你鼻子里飞出来的恶魔。没有办法知道。程序完全无效,任何行为都是可以的。根据c++标准的规则,程序员有责任调用未定义的行为-它可能编译并运行,但是你已经违反了规则,所以编译器没有义务做任何相同的事情(甚至任何事情)。