最后一个节点未打印在链接列表中

发布于 2025-02-08 04:07:22 字数 863 浏览 4 评论 0原文

我试图从列表的开头学习链接列表并执行插入操作。在打印节点时,未打印第一个节点。这是我写的核心功能。有人可以帮我吗?

struct Node //basic structure for a node
{
    ll data; //data which we want to store
    Node* link; //address of the next node;
};

Node* head=NULL;

void Insert(ll x) //insertion at beginning
{
    Node* temp=new Node();
    temp->data=x;
    temp->link=head; //we are linking new node with previously connected node
    head=temp;
}

void Print()
{
    Node* temp=head;
    while(temp->link!=NULL) //traversing the list until last element(last element.link = NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->link;
    }
    cout<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    f(i,0,5)
    {
        ll x;cin>>x;
        Insert(x);
    }
    Print();
    return 0;
}

I was trying to learn the Linked list and perform insertion operations from beginning of the list. while printing the nodes, the first node is not printed. Here is the core functions which I have written. Can someone help me?

struct Node //basic structure for a node
{
    ll data; //data which we want to store
    Node* link; //address of the next node;
};

Node* head=NULL;

void Insert(ll x) //insertion at beginning
{
    Node* temp=new Node();
    temp->data=x;
    temp->link=head; //we are linking new node with previously connected node
    head=temp;
}

void Print()
{
    Node* temp=head;
    while(temp->link!=NULL) //traversing the list until last element(last element.link = NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->link;
    }
    cout<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    f(i,0,5)
    {
        ll x;cin>>x;
        Insert(x);
    }
    Print();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月光色 2025-02-15 04:07:22

您的打印函数要求链接最后一个节点,否则不会打印。由于最后一个节点是从未链接的,因此永远不会打印。

void Print()
{
    Node* temp = head;

    while(temp)     // <- corrected condition
    {
        std::cout << temp->data << ' ';
        temp = temp->link;
    }
    std::cout << '\n';
}

Your Print function requires that the last node is linked or it won't be printed. Since the last node is never linked, it will never be printed.

void Print()
{
    Node* temp = head;

    while(temp)     // <- corrected condition
    {
        std::cout << temp->data << ' ';
        temp = temp->link;
    }
    std::cout << '\n';
}
梦幻的味道 2025-02-15 04:07:22

这是因为您的检查时间。该节点将将链接集作为null,因此它将在不打印的情况下退出。我的建议是将while检查更改为(temp!= null),但您也可以通过放置cout&lt;&lt;也可以修复它。临时数据;循环之后

It's because of your check in the while. The node will have link set as NULL, and therefore it will exit the while without printing it. My recommendation is changing the while check to (temp != NULL), but you can also fix it by putting a cout << temp->data; after the loop

若无相欠,怎会相见 2025-02-15 04:07:22

通常,函数print可以调用未定义的行为,因为它被称为空列表,因为expression temp-&gt; link使用空指针来访问内存。

另一个副作用是,由于段循环中的条件(如果列表只有一个节点,那么将不会输出其值),将跳过最后

while(temp->link!=NULL)

节点

std::ostream & Print( std::ostream &os = std::cout )
{
    for ( const Node *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }

    return os << "null";
}

一个 一样称呼

Print() << '\n';

可以像功能灵活 。您可以使用它在提供相应的文件流的文件中写入数据。

函数插入可以通过以下方式关注以下方式

void Insert( ll x ) //insertion at beginning
{
    head = new Node { x, head };
}

注意,在全局名称空间中声明指针head是一个坏主意。在这种情况下,所有函数都取决于全局变量,例如,您无法在程序中使用两个列表。

因此,您应该在Main中声明指针。

int main()
{
    Node *head = nullptr;
    //...

例如,在这种情况

void Insert( Node * &head, ll x ) //insertion at beginning
{
    head = new Node { x, head };
}

Insert( head, x );

In general the function Print can invoke undefined behavior when it is called for an empty list due to the expression temp->link that uses a null pointer to access memory.

Another side effect is that the last node will be skipped due to the condition in the while loop (if the list has only one node then its value will not be outputted)

while(temp->link!=NULL)

The function can be declared and defined the following way

std::ostream & Print( std::ostream &os = std::cout )
{
    for ( const Node *current = head; current != nullptr; current = current->next )
    {
        os << current->data << " -> ";
    }

    return os << "null";
}

And in main the function can be called like

Print() << '\n';

The function is flexible. You can use it to write data in a file providing a corresponding file stream.

The function Insert can be simplified the following way

void Insert( ll x ) //insertion at beginning
{
    head = new Node { x, head };
}

Pay attention to that it is a bad idea to declare the pointer head in the global namespace. In this case all functions depend on the global variable and you can not for example to use two lists in your program.

So you should declare the pointer in main.

int main()
{
    Node *head = nullptr;
    //...

In this case for example the function Insert can look the following way

void Insert( Node * &head, ll x ) //insertion at beginning
{
    head = new Node { x, head };
}

and called in main like

Insert( head, x );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文