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;
}

错误代码

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;
}

运行结果

图片说明

中间有错误呀,多动手,再仔细检查一遍嘛,如果还没发现,告诉你,我先运行一下