删除项目 - 使用尾递归的双向链接列表

时间:2016-12-10 14:19:41

标签: java algorithm linked-list doubly-linked-list

我目前正在学习双重链接列表。

我设法转换写了一个几乎100%功能的双向链表。但是我需要学习如何使用尾递归来编写它。

以下是我的DLLNode代码:

public class DLLNode
{
    private DLLNode previous;
    public DLLNode next;
    private String value;

    public DLLNode(String value)
    {
        this.value = value;
        this.previous = previous;
        this.next = next;
    }

    public DLLNode(String value, DLLNode next, DLLNode previous)
    {
        this.value = value;
        this.next = next;
        this.previous = previous;
    }

    public String GetDataItem()
    {
        return value;
    }

    public void setDataItem()
    {
        this.value = value;
    }


    public DLLNode GetPreviousNode()
    {
        return previous;
    }

    public void setPrevious(DLLNode previous)
    {
        this.previous = previous;
    }


    public DLLNode GetNextNode()
    {
        return next;
    }

    public void setNextNode(DLLNode next)
    {
        this.next = next;
    }

    public void addItem(String value) {
        if(this.next == null) {
            // Stopping condition
            DLLNode newNode = new DLLNode(value);
            this.next = newNode;
        } else {
            // Recurse
            this.next.addItem(value);
        }
    }

}

我设法让我的AddItem使用尾递归工作,我现在正在考虑让删除项工作。我猜测像addItem我需要deleteItem添加到我的DLLNode。

下面是我的DoublyLinkedList类:

public class DoublyLinkedList
{
    private int noOfItems;
    private DLLNode head;
    private DLLNode tail;

    // Default constructor
    public DoublyLinkedList()
    {
        head = null;
        tail = null;
        this.noOfItems = 0;
    }



    public void DeleteItem(int index)
    {
        if (index ==0)
        {
            System.out.println("Out of Bounds");
        }
        if (index > noOfItems)
        {
            System.out.println("Out of Bounds");
        }
        if (head == null)
        {
            System.out.println("No Item to remove");
        }
        else if (index == 1)
        {
            head = head.GetNextNode();
            noOfItems--;
        }
        else
        {
            int position = 0;
            DLLNode currentNode = head;

            while (currentNode != null) {
                if (position == index-1) {
                    currentNode.setNextNode(
                            currentNode.GetNextNode().GetNextNode());
                    noOfItems--;
                    break;
                }
                currentNode = currentNode.GetNextNode();
                position++;
            }
        }
    }
}

我将非常感谢有关转换此代码的任何提示。

亲切的问候, 本。

P.S。对代码格式化的方式表示歉意 - 我已经尝试修复它但它似乎无法排序。任何善于格式化代码的人都可以尝试将其整理出来吗?

2 个答案:

答案 0 :(得分:0)

private void DeleteItemHelper(final int indexToDelete, int curIndex, DLLNode curNode) {
    if (curIndex == indexToDelete) {
        // Handle removing a node with both a previous and next nodes.
    }

    else {
        DeleteItemHelper(indexToDelete, curIndex + 1, curNode.getNextNode());
    }
}


public void DeleteItem(int index) {
    DeleteItemHelper(index, 0, head);
}

答案 1 :(得分:0)

如果没有进一步测试,我认为您忘记在删除的节点后重新设置节点的头引用:

if (position == index-1) {
    // Tail of currentNode is set to the node following
    // next node, but head of that node still points to the
    // node which should be removed from list
    currentNode.setNextNode(
        currentNode.GetNextNode().GetNextNode());
    noOfItems--;
    break;
}