簡體   English   中英

在LinkedList中實現AddAtIndex方法

[英]Implementing an AddAtIndex method in a LinkedList

目前,我正在實現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++;
        }

    }

我在代碼背后的思考過程是該方法創建了一個新的Node,然后替換了鏈表的指定索引處的數據。 但是,要替換的節點上的數據存儲在一個臨時節點中,該位置在位置上遞增到新節點之后的下一個節點。 盡管代碼看起來有些草率,但我對自己的實現有80%的信心。 我創建了一個驅動程序來演示實現。 驅動程序代碼如下:

public class LinkedListDriver {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyLinkedList<String> nameList = new MyLinkedList<String>();
        nameList.add("Hamadi");
        nameList.add("Ballo");
        nameList.add(1, "Salisu");
        nameList.add(2, "Galo");
        System.out.println(nameList.toString());
        System.out.println(nameList.size());
        nameList.set(2, "Abdullahi");
        System.out.println(nameList.toString());
        nameList.remove(1);
        System.out.println(nameList.toString());
        MyLinkedList<Integer> list1 = new MyLinkedList<Integer>();
        list1.add(65);
        list1.add(21);
        list1.add(42);
        System.out.println(list1.toString());
        list1.remove(0);
        System.out.println(list1.toString());
    }

}

驅動程序的輸出如下:

List: Hamadi, Salisu, Galo, Ballo, 
4
Replacing Galo with Abdullahi
List: Hamadi, Salisu, Abdullahi, Ballo, 
Removing Salisu from the list
List: Hamadi, Abdullahi, Ballo, 
List: 65, 21, 42, 
Removing 65 from the list
List: 21, 42, 

單元測試失敗,但是出現以下錯誤:

它在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 只需創建newNode並將其正確插入currentNode及其先前節點之間即可。

您還應該考慮在列表的開頭(無上一個)或結尾(無下一個)添加元素的可能性。

我使用頭和尾作為前哨節點。 創建一個要添加到列表中的新節點。

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;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM