链表中指针的问题

ddduyong 2004-07-28 08:09:02
请看下面的程序:
#include<iostream.h>
class CAT
{
public:
CAT (){itsAge=1;}
CAT(int age):itsAge(age){}
~CAT(){};
int GetAge()const{return itsAge;}
private:
int itsAge;
};
#if !defined(AFX_NODE_H__67D9A589_1C2B_449F_B9FB_FEF64A828786__INCLUDED_)
#define AFX_NODE_H__67D9A589_1C2B_449F_B9FB_FEF64A828786__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Node
{
public:

Node();
Node(CAT*);
virtual ~Node();
void SetNext (Node*node){itsNext=node;}
Node*GetNext()const{return itsNext;}
CAT*GetCat()const{return itsCat;}
void Insert(Node*);
void Display();

private:
CAT*itsCat;
Node*itsNext;


};
#include "Node.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Node::Node(CAT*pCat):
itsCat(pCat),
itsNext(0)

{

}

Node::~Node()
{
cout<<"deleting node...\n;";

delete itsCat;
itsCat=0;
delete itsNext;
itsNext=0;
}

void Node::Insert(Node *newNode)
{
if (!itsNext)
itsNext=newNode;
else
{
int NextCatAge=itsNext->GetCat()->GetAge();
int NewAge=newNode->GetCat()->GetAge();
int ThisNodeAge=itsCat->GetAge();


if (NewAge>ThisNodeAge&&NewAge<NextCatAge)
{
newNode->SetNext(itsNext);
itsNext=newNode;
}
else
itsNext->Insert(newNode);
}
}

void Node::Display()
{
if (itsCat->GetAge()>0)
cout<<"Mycat is"<<itsCat->GetAge()<<"years old\n";
if (itsNext)
itsNext->Display();
}


#endif // !defined(AFX_NODE_H__67D9A589_1C2B_449F_B9FB_FEF64A828786__INCLUDED_)
void main()
{
Node*pNode=0;
CAT *pCat=new CAT(0);
int age;
Node *pHead=new Node(pCat);
while(1)
{
cout<<"new cat's age?(0 to quit):";
cin>>age;
if(!age)
break;
pCat=new CAT (age);
pNode=new Node(pCat);
pHead->Insert(pNode);
}
pHead->Display();
delete pHead;
cout<<"Existing...\n\n";
}

教科书上说if (NewAge>ThisNodeAge&&NewAge<NextCatAge)为真的话,通过
newNode->SetNext(itsNext);
itsNext=newNode;
上面两句就能实现新Node指向当前Node所指向的同一位置,当前Node指向新Node.
我不明白itsNext已经有了地址,再用newNode->SetNext(itsNext);会发生什么,
再用itsNext=newNode;又会发生什么,请高手指点,这个程序我已经看了三天了,还是不明白它是如何运行的。
...全文
131 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ddduyong 2004-10-20
  • 打赏
  • 举报
回复
请问这是什么问题,怎样解决

fatal error C1083: Cannot open precompiled header file: 'Debug/exmess.pch': No such file or directory
sssqin 2004-07-28
  • 打赏
  • 举报
回复
首先你要清楚:

newNode->SetNext(itsNext);//SetNext()函数的调用者是newNode对象

在SetNext()函数内部:SetNext (Node*node){itsNext=node;}
itsNext实际上是newNode->itsNext, 而这里的node是itsNext.

那么这个itsNext又是什么呢, 显然它是调用Insert()函数的链表节点对象的一个成员变量。该变量指向当前节点的下一个节点。
plusKid 2004-07-28
  • 打赏
  • 举报
回复
这是教科书上的?怎么好像Node的默认构造函数Node()没有实现代码呀?还有
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
这些乱七八糟的东西。
不过,言归正传,那两句语句确实可以实现楼主所说的功能:
原来:
this->itsNext------------------->Next
调用newNode->SetNext(itsNext);之后,就是this->itsNext和newNode->itsNext同时指向Next。而再调用itsNext=newNode;也就是this->itsNext=newNode;之后,就变成:
this->itsNext------------------->newNode->itsNext------------------------>Next

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧