星际争霸网站:函数参数是指针类型,为何调用该函数后,没有发生变化

来源:百度文库 编辑:中科新闻网 时间:2024/04/29 15:56:15
类queue有一个出队头的函数如下,其参数类型是指针类型的,调用该函数后为何指针所指向的节点没有发生变化呢?(最后一个数值错误,不知道为什么)

#include "iostream.h"
//队列节点
struct QNode
{
int weight;
QNode *next;
QNode *parent;
bool LChild;
};
///类
class queue
{
private:
QNode* front;
QNode* rear;
public:
queue();
~queue();
void Add(QNode *);
void Delete(QNode *);
bool IsEmpty();
};
////函数
//入队
void queue::Add(QNode* nodePtr)
{
rear->next=nodePtr;
rear=nodePtr;
cout<<nodePtr->weight<<"enqueue"<<endl;
}
queue::queue()
{
front=rear=new QNode;
front->next=NULL;
}

//////////
//析构函数
queue::~queue()
{
front->next=NULL;
delete front,rear;
}
void queue::Delete(QNode* nodePtr)
{
if(front==rear)//队列已经为空
{
cout<<"the queue has no element already"<<endl;
}
else //正常情况
{
nodePtr=front->next;
front->next=nodePtr->next;
if(rear==nodePtr)
rear=front;
cout<<"element "<<nodePtr->weight<<" deleted"<<endl;
}
}
void main()
{
queue Q;
QNode *tagNode;
tagNode=new QNode;
tagNode->LChild=true;
tagNode->next=NULL;
tagNode->parent=NULL;
tagNode->weight=-1;
Q.Add(tagNode);
QNode* rootNode=new QNode;
rootNode->weight=0;
rootNode->parent=NULL;
rootNode->LChild=false;
rootNode->next=NULL;
Q.Add(rootNode);
QNode* E=tagNode;
Q.Delete(E);//
cout<<E->weight<<endl;
Q.Delete(E);//E存储的是出队的元素
cout<<E->weight<<endl;//这里的E应该是删除的新节点阿,可
//是为什么没有变化呢
}