Track, 2019-05-15 16:31 采纳率: 100%
浏览 325
已采纳

c++单链表创建新结点时不能输出值

才开始学数据结构,复现单链表的时候出现问题。。。
当我打算在头结点后面再创一个值为1的结点时,输出结果显示链表长度增加1,但是不能输出新增结点的属性,代码如下,求大佬解答

#include<iostream>
using namespace std;
struct LinkNode {    //链表结点
    int data;
    LinkNode* link;
    LinkNode()
    {
        this->link = NULL;
        this->data = 0;
    }
    LinkNode(const int& data, LinkNode* link = NULL)
    {
        this->data = data;
        this->link = link;
    }
};

class Link :public LinkNode {     //单链表
private:
    LinkNode* first;
public:
    Link()
    {
        first = new LinkNode;
    }
    Link(const int& d)
    {
        first = new LinkNode(d);
    }
    int lenth();  //返回列表长度
    LinkNode* Locate(int i);  //定位
    bool Create(int d);  //创造一个新结点
};

LinkNode* Link::Locate(int i)
{
    if (i < 0) 
    {
        return false;
    }
    int count = 0;
    LinkNode* current = first;
    while (count < i&&current->link!=NULL)
    {
        current = current->link;
        count++;
    }
    return current;
}
int Link::lenth()
{
    int lenth = 0;
    LinkNode* current = first;
    while (current->link != NULL)
    {
        lenth++;
        current = current->link;
    }
    return lenth;
}
bool Link::Create(int d)
{
    LinkNode* current = Locate(lenth());
    LinkNode* newnode = new LinkNode(d);
    if (newnode == NULL && current == NULL)
    {
        cout << "存储分配错误" << endl;
        exit(1);
    }
    current->link = newnode;
    return true;
}

int main()
{
    Link* a = new Link;
    cout << a->data << ' ' << a->lenth() << endl;
    a->Create(1);  //创建一个值为1的结点
    cout << a->lenth() << endl;
    cout << a->link->data;
    return 0;
}
  • 写回答

2条回答 默认 最新

  • 北冥有鱼wyh 2019-05-16 09:43
    关注

    错误代码

    cout << a->link->data;
    

    错误原因

    a是1条链表,而first是链表首节点,link是first中的成员,first是a中的成员。a->first->link才是指向新增节点的地址
    故输出新增节点的属性,正确代码应为:

    cout << a->first->data;
    

    而在class Link中first是private,不可由外部函数访问,若想直接访问,可将其改为public。也通过其它成员函数间接访问。

    完整代码

    #include<iostream>
    using namespace std;
    struct LinkNode {    //链表结点
        int data;
        LinkNode* link;
        LinkNode()
        {
            this->link = NULL;
            this->data = 0;
        }
        LinkNode(const int& data, LinkNode* link = NULL)
        {
            this->data = data;
            this->link = link;
        }
    };
    
    class Link :public LinkNode {     //单链表
    public:
        LinkNode* first;
        Link()
        {
            first = new LinkNode;
        }
        Link(const int& d)
        {
            first = new LinkNode(d);
        }
        int lenth();  //返回列表长度
        LinkNode* Locate(int i);  //定位
        bool Create(int d);  //创造一个新结点
    };
    
    LinkNode* Link::Locate(int i)
    {
        if (i < 0) 
        {
            return false;
        }
        int count = 0;
        LinkNode* current = first;
        while (count < i&&current->link!=NULL)
        {
            current = current->link;
            count++;
        }
        return current;
    }
    int Link::lenth()
    {
        int lenth = 0;
        LinkNode* current = first;
        while (current->link != NULL)
        {
            lenth++;
            current = current->link;
        }
        return lenth;
    }
    bool Link::Create(int d)
    {
        LinkNode* current = Locate(lenth());
        LinkNode* newnode = new LinkNode(d);
        if (newnode == NULL && current == NULL)
        {
            cout << "存储分配错误" << endl;
            exit(1);
        }
        current->link = newnode;
        return true;
    }
    
    int main()
    {
        Link* a = new Link;
        cout << a->data << ' ' << a->lenth() << endl;
        a->Create(1);  //创建一个值为1的结点
        cout << a->lenth() << endl;
        cout << a->first->data;
        return 0;
    }
    

    运行结果

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?