链表的'set'方法的实现?

Implementation of the 'set' method for a linked List?

我正在尝试实现 set 方法,您可以在该方法中传递所需的链接列表中的位置和值,然后 set 函数将该值添加到链接列表中指定的位置。我已经实现了 set 函数,但由于某种原因最后一个元素在我的实现中消失了。我将不胜感激任何帮助。提前致谢。我将不胜感激任何能看到我遗漏的专家的眼睛。


/**
 * A basic singly linked list implementation.
 */
public class SinglyLinkedList<E> implements Cloneable, Iterable<E>, List<E> {
    //---------------- nested Node class ----------------

    /**
     * Node of a singly linked list, which stores a reference to its
     * element and to the subsequent node in the list (or null if this
     * is the last node).
     */
    private static class Node<E> {
       E value;
       Node<E> next;
       public Node(E e) 
       { 
           value = e; 
           next = null; 
       } 
    }
    
    //----------- end of nested Node class -----------

    // instance variables of the SinglyLinkedList
    private Node<E> head = null; // head node of the list (or null if empty)

    private int size = 0; // number of nodes in the list

    public SinglyLinkedList() {
    }              // constructs an initially empty list

    // access methods

    /**
     * Returns the number of elements in the linked list.
     *
     * @return number of elements in the linked list
     */
    public int size() {
        return size;
    }
    /**
     * Adds an element to the end of the list.
     *
     * @param e the new element to add
     */
    public void addLast(E e) {
        // TODO
    }
    /**
     * Tests whether the linked list is empty.
     *
     * @return true if the linked list is empty, false otherwise
     */
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public E get(int i) throws IndexOutOfBoundsException {
        Node<E> a = head;
        if(i<=this.size()) {
            int count = 0;
            while(count < i) {
                count ++;
                a = a.next;
            }
            return a.value;
        }

        return null;
    }

    @Override
    public E set(int i, E e) throws IndexOutOfBoundsException {
        Node<E> current = head;
        Node<E> setNode = new Node<E>(e);
        if(i==0) {
            this.addFirst(e);
        }
        else if(i==this.size){
            this.addLast(e);
        }
        else {
            for(int j=0; current != null && j < (i-1);j++) {
                current = current.next;
            }
            Node<E> temp = current.next;
            current.next = setNode;
            setNode.next = temp;
        }
        return setNode.value;
        }
    
    

    

   

    // update methods

    /**
     * Adds an element to the front of the list.
     *
     * @param e the new element to add
     */
    public void addFirst(E e) {
        Node<E> first = new Node<>(e);
        first.next = this.head;
        this.head = first;
        this.size++;
    }

   

    
    @SuppressWarnings({"unchecked"})
    public boolean equals(Object o) {
        // TODO
        return false;   // if we reach this, everything matched successfully
    }

    @SuppressWarnings({"unchecked"})
    public SinglyLinkedList<E> clone() throws CloneNotSupportedException {
        // TODO
        return null;
    }


    /**
     * Produces a string representation of the contents of the list.
     * This exists for debugging purposes only.
     * @return 
     */
    public String toString() {
        for(int i=0;i<this.size();i++) {
            System.out.println(this.get(i));
        }
        return "end of Linked List";
    }

    public static void main(String[] args) {
        
            SinglyLinkedList <Integer> ll =new SinglyLinkedList <Integer>();
            ll.addFirst(5);
            ll.addFirst(4);
            ll.addFirst(3);
            ll.addFirst(2);
            ll.set(1,0);
            System.out.println(ll);
        }
    }

假设

  1. addLast 代码中缺少方法
  2. 错误也可能存在

已知原因

此代码不会递增 set 方法中的 size for loop.The sizeaddLast 中递增(可能)和 addFirst 并且在其他情况下不递增(最后 else 部分)

尚不清楚计划使用 set 方法做什么。它现在的实现方式,它的行为更像 insert,这意味着它会添加新节点,但不会增加元素的总数 (size)。

如果set方法改变了节点的值,名字表示,那么这部分是错误的:

else {
    for(int j=0; current != null && j < (i-1);j++) {
        current = current.next;
    }
    Node<E> temp = current.next;
    current.next = setNode;
    setNode.next = temp;
}

它应该替换值而不是添加新值:

else {
    for(int j=0; current != null && j < (i-1);j++) {
        current = current.next;
    }
    current.value=e
}

此外,看起来索引 i 是从 1 开始的,而其他所有内容都是从 0 开始的。我没有检查上面的代码,但是概念应该是这样的。