Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在LinkedList中实现AddAtIndex方法_Java_Data Structures_Linked List_Implementation - Fatal编程技术网

Java 在LinkedList中实现AddAtIndex方法

Java 在LinkedList中实现AddAtIndex方法,java,data-structures,linked-list,implementation,Java,Data Structures,Linked List,Implementation,目前,我正在努力实现一个AddAtIndex方法,在大多数情况下,它似乎工作得很好。然而,我的方法没有通过JUnit测试,我似乎不明白为什么。因此,我选择展示我迄今为止所做的代码: ** * Add an element to the list at the specified index * @param The index where the element should be added * @param element The element to add

目前,我正在努力实现一个AddAtIndex方法,在大多数情况下,它似乎工作得很好。然而,我的方法没有通过JUnit测试,我似乎不明白为什么。因此,我选择展示我迄今为止所做的代码:

**
     * Add an element to the list at the specified index
     * @param The index where the element should be added
     * @param element The element to add
     */
    public void add(int index, E element ) //Method should be O(1) time.
    {
        // TODO: Implement this method
        if (index < 0) { 
            System.out.println("Can't add an element at a negative index.");
        }
        int i = 0;
        LLNode<E> currentNode = head.next;
        while ( i < size ) {
            if ( i == index ) {
                LLNode<E> newNode = new LLNode<E>(element);
                LLNode<E> tempNode = new LLNode<E>(currentNode.data);

                currentNode.next = tempNode;
                currentNode.data = newNode.data;

                newNode.prev = currentNode.prev;
                newNode.next = tempNode;
                tempNode.prev = newNode;
                size++;
            }
            currentNode = currentNode.next;
            i++;
        }

    }
但是,单元测试失败,出现以下错误:

它在AssertEquals方法中失败:

shortList.add(2, "E");
        shortList.add(3, "F");
        **assertEquals("AddAtIndex: at position 2 ", "E", shortList.get(2)); //fails here**
        assertEquals("AddAtIndex: at position 3 ", "F", shortList.get(3));
        assertEquals("AddAtIndex: List size is ", 6, shortList.size());

我想知道我做错了什么。虽然我知道我的AddAtindex方法有点不对劲,但我已经完全明白了这一点。谢谢

您不需要这个
tempNode
。只需创建
新节点
,并将其正确插入
当前节点
及其上一个节点之间


你也应该考虑在列表的开头(前一个)或结束(没有下一个)添加元素的可能性。

< P>你不需要那个代码> TimeNo.<代码>。只需创建
新节点
,并将其正确插入
当前节点
及其上一个节点之间

public boolean add(E element) {
        // create new element
        LLNode<E> variable = new LLNode(element);
        variable.next = null;
        variable.prev = null;

        // if element is null, throw exception
        if (element == null) {
            // return false;
            throw new NullPointerException("Element is null");
        } else {
            // get the value stored in tail.prev in variable temp. 
            variable.prev = tail.prev;
            variable.next = tail;
            // now modify the tail node prev and new node next
            tail.prev = variable;

            // get prev node next link changed
            variable.prev.next = variable;

            // update size
            if (head.next.next != tail) {
                size++;
            }
            return true;
        }
    }

你也应该考虑在列表的开头(前一个)或末尾(没有下一个)添加元素的可能性。

< P>我用头和尾作为前哨节点。已创建要添加到列表中的新节点

public boolean add(E element) {
        // create new element
        LLNode<E> variable = new LLNode(element);
        variable.next = null;
        variable.prev = null;

        // if element is null, throw exception
        if (element == null) {
            // return false;
            throw new NullPointerException("Element is null");
        } else {
            // get the value stored in tail.prev in variable temp. 
            variable.prev = tail.prev;
            variable.next = tail;
            // now modify the tail node prev and new node next
            tail.prev = variable;

            // get prev node next link changed
            variable.prev.next = variable;

            // update size
            if (head.next.next != tail) {
                size++;
            }
            return true;
        }
    }
公共布尔添加(E元素){
//创建新元素
LLNode变量=新的LLNode(元素);
variable.next=null;
variable.prev=null;
//若元素为null,则抛出异常
if(元素==null){
//返回false;
抛出新的NullPointerException(“元素为null”);
}否则{
//获取存储在变量temp中tail.prev中的值。
variable.prev=tail.prev;
variable.next=tail;
//现在修改tail节点prev和new节点next
tail.prev=变量;
//获取上一个节点下一个链接已更改
variable.prev.next=变量;
//更新大小
if(head.next.next!=tail){
大小++;
}
返回true;
}
}

我使用头部和尾部作为哨兵节点。已创建要添加到列表中的新节点

public boolean add(E element) {
        // create new element
        LLNode<E> variable = new LLNode(element);
        variable.next = null;
        variable.prev = null;

        // if element is null, throw exception
        if (element == null) {
            // return false;
            throw new NullPointerException("Element is null");
        } else {
            // get the value stored in tail.prev in variable temp. 
            variable.prev = tail.prev;
            variable.next = tail;
            // now modify the tail node prev and new node next
            tail.prev = variable;

            // get prev node next link changed
            variable.prev.next = variable;

            // update size
            if (head.next.next != tail) {
                size++;
            }
            return true;
        }
    }
公共布尔添加(E元素){
//创建新元素
LLNode变量=新的LLNode(元素);
variable.next=null;
variable.prev=null;
//若元素为null,则抛出异常
if(元素==null){
//返回false;
抛出新的NullPointerException(“元素为null”);
}否则{
//获取存储在变量temp中tail.prev中的值。
variable.prev=tail.prev;
variable.next=tail;
//现在修改tail节点prev和new节点next
tail.prev=变量;
//获取上一个节点下一个链接已更改
variable.prev.next=变量;
//更新大小
if(head.next.next!=tail){
大小++;
}
返回true;
}
}

使用调试器,看看指针是如何从初始输出移动的,我认为add方法基本上是有效的。我不确定get方法是如何返回一个错误的值的,尽管您还应该了解
for
-循环:
for(int I=0;I
使用调试器,看看指针是如何从初始输出移动的,我认为add方法基本上是有效的。我不确定get方法是如何返回一个坏值的,尽管您还应该了解
for
-循环:
for(int I=0;I
开头有一个元素,它是一个称为head的空哨兵节点。我应该说得更具体些,谢谢!开头有一个元素,它是一个称为head的空哨兵节点。我应该说得更具体些,谢谢!