从最后一个节点,首先遍历指针 [英] traverse pointer from last node to first

查看:127
本文介绍了从最后一个节点,首先遍历指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{

    while(temp->next!=current)
    {
        temp=temp->next;
        current=temp;
    }
    temp=head;

    return current; 
}

我链表头指针举办第一个节点,当前指针保持最后一个节点,我希望把当前头一个接一个,所以我写这篇文章的功能,但它给我调试段故障程序

I have linked list and 'head' pointer hold first node , 'current' pointer hold last node,I want to bring 'current' to head one by one ,so I write this function but it gives segmentation fault when I debug the program

推荐答案

TEMP-&G​​T;!下次=电流将永远是真实的,除非温度== TEMP->接着

temp->next!=current will never be true unless temp==temp->next.

试试这个:

bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{

    while(temp->next!=current)
    {
        temp=temp->next;
    }
    current=temp; /* get this out of the loop */
    temp=head;

    return current; 
}

以上简化了这个:

or more simplified this:

bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{
    (void)head; /* head isn't used, so put this to avoid warning */
    while(temp->next!=current)
    {
        temp=temp->next;
    }
    /* current and temp will be lost, so assigning to them here is useless */

    return temp; 
}

要使它更安全,确保温度不是 NULL 来避免的情况下,运行时错误电流是无效的。

To make it safer, make sure that temp isn't NULL to avoid runtime error in case of current is invalid.

bn_ptr drive_temp(bn_ptr head,bn_ptr temp,bn_ptr current)
{
    (void)head; /* head isn't used, so put this to avoid warning */
    while(temp != NULL && temp->next!=current)
    {
        temp=temp->next;
    }

    /* temp will be the previous node of current or NULL */
    return temp; 
}

也许你想这样的:

Maybe you want this:

bn_ptr drive_temp(bn_ptr head,bn_ptr current) /* remove temp from the arguments */
{
    bn_ptr temp = head;
    while(temp != NULL && temp->next!=current)
    {
        temp=temp->next;
    }

    /* temp will be the previous node of current or NULL */
    return temp; 
}

这篇关于从最后一个节点,首先遍历指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆