如何改进此RemoveAt [英] How to improve this RemoveAt

查看:93
本文介绍了如何改进此RemoveAt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要一些帮助来改进这段代码,以便从特定位置的链表中删除节点。



 ///< summary> 
///从特定位置移除节点
///< / summary>
///< param name =nodePosition>< / param>
///< returns>< / returns>
public bool RemoveAt(int nodePosition)
{
//选择的节点超出界限
if(nodePosition> listSize-1 || nodePosition< 0)
{
返回false;
}

//第一个节点选择
if(nodePosition == 0)
{
if(listSize == 1)
{
first = null;
listSize = 0;
返回true;
}
其他
{
first = first.Next;
}
}

节点node = first;

//运行列表
for(int i = 0; i< nodePosition; i ++)
{
//如果它在开头或最后
if(i == listSize - 1)
{
node.Next = null;
休息;
}

if(i == nodePosition - 1)
{
node.Next = node.Next.Next;
休息;

}
else
{
node = node.Next;
}

}

listSize--;
返回true;
}

解决方案

这是一种非常奇怪的方法。我根本不关心列表大小。



我建议采用两阶段方法,首先确定要删除的节点,然后删除它。保持一致性的一个好方法是在列表的开头有一个空的虚拟节点,纯粹是为了提供下一个项目的链接(第一个项目或null),如果没有的话。通过这种方式,您可以编写一个通用方法,该方法不必对第一个项目表现不同。



节点当前=虚拟; //前面的空节点

while(current.Next!= null&& index--)
{
current = current.Next;
}



这将停止在您要删除的项目之前的项目,所以为了得到它你只需要current.Next这将是项目或如果你已经离开了结构,那就是null。



然后(如果current.Next!= null):



 current.Next = current.Next.Next; //删除了项目!





建立算法时最好避开边缘情况下的特殊块(即第一项,最后一项)等)


Hello everyone,

I need some help to improve this bit of code to remove a node from a linked list at a specific location.

/// <summary>
/// Removes a node from a specific location
/// </summary>
/// <param name="nodePosition"></param>
/// <returns></returns>
public bool RemoveAt(int nodePosition)
{
    //The choosen node is out of bounds
    if (nodePosition > listSize-1 || nodePosition < 0)
    {
        return false;
    }

    //first node was choosen
    if (nodePosition == 0)
    {
        if (listSize == 1)
        {
            first = null;
            listSize = 0;
            return true;
        }
        else
        {
            first = first.Next;
        }
    }

    Node node = first;

    //runs through the list
    for (int i = 0; i < nodePosition; i++)
    {
        //if it is at the beginning or at the end
        if (i == listSize - 1)
        {
            node.Next = null;
            break;
        }

        if (i == nodePosition - 1)
        {
            node.Next = node.Next.Next;
            break;

        }
        else
        {
            node = node.Next;
        }

    }

    listSize--;
    return true;
}

解决方案

That's a very odd way to do it. I wouldn't concern yourself with list size at all.

I'd recommend a two stage approach, first identifying the node you want to remove, the removing it. A good way to keep things consistent is to have an empty dummy node at the beginning of the list, purely to provide the link to the next item (the first item or null) if there isn't one. This way you can write a generalised method which doesn't have to behave differently for the first item.

Node current = dummy; // empty node at front

while (current.Next != null && index--)
{
    current = current.Next;
}


this will stop at the item before the one you want to remove, so to get it you just need current.Next which will be the item or null if you've gone outside the structure.

Then (if current.Next != null):

current.Next = current.Next.Next; // item removed!



It's best when building alogithms to steer clear of special blocks for edge cases (ie. first item, last item etc.)


这篇关于如何改进此RemoveAt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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