| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 745 人关注过本帖
标题:菜鸟求助,大侠们多帮帮忙哦
只看楼主 加入收藏
liel
Rank: 1
等 级:新手上路
帖 子:115
专家分:0
注 册:2007-1-16
收藏
 问题点数:0 回复次数:6 
菜鸟求助,大侠们多帮帮忙哦
我的问题是:链表类里的成员函数看不懂,大侠们帮忙解释一下什么意思哈!!
多谢多谢哦!

下面是删除一个节点的成员函数,
节点类和链表类的定义
中间红色标签的不懂是什么意思。

//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]]
搜索更多相关主题的帖子: 那边 
2008-09-19 22:09
liel
Rank: 1
等 级:新手上路
帖 子:115
专家分:0
注 册:2007-1-16
收藏
得分:0 
还是没有明白,来人帮忙啊!!

2008-09-21 16:55
liel
Rank: 1
等 级:新手上路
帖 子:115
专家分:0
注 册:2007-1-16
收藏
得分:0 
up up

2008-09-22 16:40
没有道理
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-7-3
收藏
得分:0 
  temp->readp()->setn(pcurrent->readn());      //这句是什么意思啊??”

根据字面的理解,p是prew   n是next,也就是上一个,和下一个.
我的理解是:temp为它的上一个节点("temp->readp()",readp()功能为读取出上一个节点),
设置其下一个节点(setn()功能应是设置下一个节点)为当前节点的下一个节点("setn(pcurrent->readn())")。

2008-09-24 08:18
ting4763
Rank: 1
等 级:新手上路
威 望:2
帖 子:44
专家分:6
注 册:2008-8-30
收藏
得分:0 
Node *temp=new Node(i,c,pcurrent,pcurrent->readn()); //创建一个新的结点
        if(pcurrent->readn()!=NULL)
        {
            pcurrent->readn()->setp(temp);     //如果pcurrent的下一个结点为空,这是设置pcurrent的下一个结点的上个结点为temp结点 其实就是如果当前结点(pcurrent)不为空,就在它与它的下一结点中插入一个新的结点temp
                    }
        pcurrent->setn(temp);
        return true;

    if(pcurrent!=NULL&&pcurrent!=&head)//head节点不能删除
    {
        Node*temp=pcurrent;
        if(temp->readn()!=NULL)
        {
            temp->readn()->setp(pcurrent->readp());    //设置temp的下一个结点的上一个结点为当然结点的上一个结点 其实就是如果temp结点的下一个结点不为空,就让它的下个结点的上一个结点指向当前结点的上一个结点
        }
        temp->readp()->setn(pcurrent->readn());      //设置temp结点的上一个结点的下一个结点为当前结点的下一个结点

        pcurrent=temp->readp();         
        delete temp;
        return true;
    }
2008-09-24 10:04
liel
Rank: 1
等 级:新手上路
帖 子:115
专家分:0
注 册:2007-1-16
收藏
得分:0 
多谢“没有道理”和“ting4763”两位兄弟了!几天的疑惑终于解决了!
多谢多谢!!

2008-09-24 10:41
快速回复:菜鸟求助,大侠们多帮帮忙哦
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018287 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved