菜鸟求助,大侠们多帮帮忙哦
我的问题是:链表类里的成员函数看不懂,大侠们帮忙解释一下什么意思哈!!多谢多谢哦!
下面是删除一个节点的成员函数,
节点类和链表类的定义
中间红色标签的不懂是什么意思。
//node.h
#include<iostream>
using namespace std;
class Node
{
public:
Node();
Node(int i,char c='0');
Node(int i,char c,Node *p,Node *n);
int readi() const;
char readc() const;
Node *readp() const;
Node *readn() const;
bool set(int i);
bool set(char c);
bool setp(Node *p);
bool setn(Node *n);
private:
int idata;
char cdata;
Node *prior;
Node *next;
};
Node::Node()
{
cout<<"Node constructor is running....."<<endl;
idata=0;
cdata='0';
prior=NULL;
next=NULL;
}
Node::Node(int i,char c)
{
cout<<"Node constructor is running....."<<endl;
idata=i;
cdata=c;
prior=NULL;
next=NULL;
}
Node::Node(int i,char c,Node *p,Node *n)
{
cout<<"Node constructor is running....."<<endl;
idata=i;
cdata=c;
prior=p;
next=n;
}
int Node::readi() const
{
return idata;
}
char Node::readc() const
{
return cdata;
}
Node *Node::readp()const
{
return prior;
}
Node *Node::readn() const
{
return next;
}
bool Node::set(int i)
{
idata=i;
return true;
}
bool Node::set(char c)
{
cdata=c;
return true;
}
bool Node::setp(Node *p)
{
prior=p;
return true;
}
bool Node::setn(Node *n)
{
next=n;
return true;
}
//Linklist.h
#include<iostream>
#include"node1.h"
using namespace std;
class Linklist
{
public:
Linklist(int i,char c);
bool Locate(int i);
bool Locate(char c);
bool Insert(int i=0,char c='0');
bool Delete();
void Show();
void Destroy();
private:
Node head;
Node *pcurrent;
};
Linklist::Linklist(int i,char c):head(i,c)//类名::构造函数名(参数表):成员对象名1(参数表),
//链表类构造函数,调用head对象的构造函数重载1,详见Node.h文件
{
cout<<"Linklist constructor is running..."<<endl;
pcurrent=&head;
}
bool Linklist::Locate(int i)
{
Node *ptemp=&head;
while(ptemp!=NULL)
{
if(ptemp->readi()==i)
{
pcurrent=ptemp;
return true;
}
ptemp=ptemp->readn();
}
return false;
}
bool Linklist::Locate(char c)
{
Node * ptemp=&head;
while(ptemp!=NULL)
{
if(ptemp->readc()==c)
{
pcurrent=ptemp;
return true;
}
ptemp=ptemp->readn();
}
return false;
}
bool Linklist::Insert(int i,char c)
{
if(pcurrent!=NULL)
{
Node *temp=new Node(i,c,pcurrent,pcurrent->readn());
if(pcurrent->readn()!=NULL)
{
pcurrent->readn()->setp(temp); //什么意思??
}
pcurrent->setn(temp);
return true;
}
else
{
return false;
}
}
bool Linklist::Delete()
{
if(pcurrent!=NULL&&pcurrent!=&head)//head节点不能删除
{
Node*temp=pcurrent;
if(temp->readn()!=NULL)
{
temp->readn()->setp(pcurrent->readp()); [color=Red]//这句是什么意思啊??[/color]
}
temp->readp()->setn(pcurrent->readn()); [color=Red]//这句是什么意思啊??
pcurrent=temp->readp(); [/color]
delete temp;
return true;
}
else
{
return false;
}
}
void Linklist::Show()
{
Node *ptemp=&head;
while(ptemp!=NULL)
{
cout<<ptemp->readi()<<'\t'<<ptemp->readc()<<endl;
ptemp=ptemp->readn();
}
}
void Linklist::Destroy()
{
Node *ptemp1=head.readn();
while(ptemp1!=NULL)
{
Node *ptemp2=ptemp1->readn();
delete ptemp1;
ptemp1=ptemp2;
}
head.setn(NULL);
}
[[it] 本帖最后由 liel 于 2008-9-19 22:14 编辑 [/it]]