Linked List实现方法,用于删除具有给定节点值的节点之前(之后)的节点

时间:2015-03-24 03:03:50

标签: c# linked-list

这是我简单删除指示节点的方法,但是如何实现DeleteBefore()和DeleteAfter()方法,该方法在具有给定节点值的节点之前(之后)删除节点。

public void DeleteAFter(T nodeVal)
    {
        Node<T> curr = front;
        Node<T> prev = null;
        // becomes true if we locate target
        bool foundItem = false;
        // scan until locate nodeVal or 
        // come to end of list
        while (curr != null && !foundItem)
        {
            // have a match 
            if (curr.NodeValue.Equals(nodeVal))
            {
                // remove the first node
                if (prev == null)
                {
                    front = front.Next;
                }
                // erase intermediate node
                else
                {
                    prev.Next = curr.Next;
                }
                foundItem = true;

            }
            else
            {
                // advanced curr and prev
                prev = curr;
                curr = curr.Next;
            }

0 个答案:

没有答案