如何删除链表中特定索引处的对象?

时间:2014-06-17 00:14:44

标签: java

我看到有关于这个主题的一些问题,但是这里的情况有点不同,而其他问题涉及Java链表类,这是我从老师那里得到的具有一些特定属性的自定义类。

所以我需要做的是编写一个名为'removeAt'的方法,它获取'int k'并删除'k'索引处的对象并返回被删除对象的'data'项... < / p>

我的问题是找到我要删除它的对象后(基本上删除它指向下一个列表节点的指针..)并且感觉有一些缺少的属性,并且他没有说要添加新的属性。 / p>

这是我的代码:

public class List {

    private ListNode firstNode;
    private ListNode lastNode;
    private String name;


    public List()
    {
        this("list");
    }


    public List(String listName)
    {
        name=listName;
        firstNode=lastNode=null;
    }


    //override 
    public String toString()
    {
        String stringToReturn = "";

        ListNode currentNode = firstNode;

        while (currentNode != null) {

            stringToReturn += currentNode.toString();
            currentNode = currentNode.getNext();

            if (currentNode != null) {
                stringToReturn += ", ";
            }
        }
        return stringToReturn;
    }


    public Object removeAt(int k) throws ListIndexOutOfBound
    {
        ListNode removedNode = this.firstNode;
        Object removedObject;
        int idx = 0;

        if (isEmpty())
            throw new EmptyListException(this.name);

        if (k < 0)
            throw new ListIndexOutOfBound();

        if (k == 0) {
            removedObject = this.removeFromFront();
        } else {

            while (idx != k) {
                removedNode = removedNode.getNext();
                idx++;

                if (removedNode == null)
                    throw new ListIndexOutOfBound();
            }

            if (removedNode == this.lastNode) {
                removedNode = (ListNode) this.removeFromBack();
            } else {
                removedObject = removedNode.data;
                //i'm stuck here...I want to remove this node from the list!

            }
        }
        return removedNode; 
    }

还有一个ListNode类,如下所示:

public class ListNode {

    Object data;
    ListNode nextNode;


    public ListNode(Object o){
        this(o,null);
    }
    public ListNode(Object o,ListNode node){
        data=o;
        nextNode=node;
    }
    public Object getObject(){
        return data;
    }
    public ListNode getNext(){
        return nextNode;
    }

}

您如何建议完成此任务?

1 个答案:

答案 0 :(得分:1)

我建议您在删除节点之前始终记住节点,以便在删除所需节点后设置下一个节点:

    if (k == 0) {
        removedObject = this.removeFromFront();
    } else {

        ListNode prev = null;

        while (idx != k) {
            prev = removedNode;
            removedNode = removedNode.getNext();
            idx++;

            if (removedNode == null)
                throw new ListIndexOutOfBound();
        }

        if (removedNode == this.lastNode) {
            removedNode = (ListNode) this.removeFromBack();
        } else {
            removedObject = removedNode.data;
            prev.setNext(removedNode.getNext());
        }
    }

这意味着您必须向setNext(ListNode next)对象添加方法ListNode

    public void setNext(ListNode next){
        nextNode = next;
    }

这样做是将nextNode的{​​{1}}设置为prev

removedNode.getNext()

后:

|                |   prev.getNext()     | removedNode.getNext()    |
--------------------------------------------------------------------
| prev           |   removedNode        | <any node> or null       |