删除节点不起作用

Delete node wont work

我试图删除链表上的一个节点,但在执行删除命令后,我试图显示节点内的数据,但我仍然可以显示应该删除的数据。搜索是我要删除的节点

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;
    }    

通过 deleted 指针读取数据是 "Undefined Behaviour" - 这意味着编译器可以为所欲为,而您的程序没有任何意义。

可能得到你期望的结果,你可能崩溃,你可能 让恶魔飞出你的鼻子。无从得知。该程序完全无效,any 行为正常。根据 C++ 标准的规则,程序员有责任 调用未定义的行为 - 它 可以 编译和 运行,但是你违反了规则,所以编译器没有义务做任何理智的事情(甚至根本没有做任何事情)。