单链表的删除。为什么它删除运行显示的是删除的地址

图片说明
#include"List.h"
#include
using namespace std;
List::List(){
m_pList=new Node;
m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;

};
List::~List(){
ClearList();
delete m_pList;
m_pList=NULL;

}
void List::ClearList(){
Node *currentNode=m_pList->next;
while(currentNode!=NULL)
{
Node *temp=currentNode->next;
delete currentNode;
currentNode=temp;
}
m_pList->next=NULL;

}
bool List::ListInsert(int i,Node *pNode)
{
if(im_iLength){
return false;
}

    Node *currentNode=m_pList;
    for(int k=0;k<i;k++)
    {
        currentNode=currentNode->next;
    }
    Node *newNode=new Node;
    newNode->data=pNode->data;
    newNode->next=currentNode->next;
    currentNode->next=newNode;
    m_iLength++;
    return true;

}

bool List::ListDelete(int i,Node *pNode)
{
if(i=m_iLength){
return false;

}
Node *currentNode=m_pList;
Node *currentNodeBefore=NULL;
for(int k=0;k<=i;k++){
    currentNodeBefore=currentNode;
    currentNode=currentNode->next;
    currentNodeBefore->next=currentNode->next;
    pNode->data=currentNode->data;
    delete currentNode;
    currentNode=NULL;
}
m_iLength--;
return true;

}
bool List::ListInsertHead(Node *pNode){
Node *temp=m_pList->next;
Node *newNode=new Node;
newNode->data=pNode->data;
m_pList->next=newNode;
newNode->next=temp;
return true;

}

void List::Tranverse()
{
Node*currentNode=m_pList;
while(currentNode->next!=NULL)
{
currentNode=currentNode->next;
cout<data<<endl;
}
}

int main()
{
List *pList=new List();
Node nodel;
nodel.data=3;

pList->ListInsert(0,&nodel);
Node node5;
node5.data=7;
pList->ListInsert(1,&node5);


cout<<"未删除节点前"<<endl;
pList->Tranverse();

Node temp;
pList->ListDelete(0,&temp);
cout<<"删除节点后"<<endl;
pList->Tranverse();

cout<<"被删除的节点是"<<&temp<<endl;

system("pause");

}

2个回答

cout<<"被删除的节点是"<<temp.data<<endl;

pat_ti
HEIMIIY 问一下为什么 我只能删除3不能删除7呀
一年多之前 回复

取地址取得是指针地址

单链表的话删除的应该是地址,如果删除值的话我觉得直接用destory

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问