有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

链表“set”方法的java实现?

我试图实现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);
        }
    }

共 (0) 个答案