LinkedList java. Single Array List implementation

Задача в том что бы реализовать методы в SimpleArrayListImpl, учитывая особые правила: нельзя использовать java.util за исключение (java.util.Iterator java.util.NoSuchElementException java.util.Objects java.util.Optional) так же нельзя добавлять поля в SingleArrayListImpl, SinglyLinkedListImpl.Node и добавлять методы в SinglyLinkedListImpl.Node за исключением конструкторов. проблема следующая не проходят четыре теста два из них testIterator при котором в лист добавляются 3-4 элемента и метод должен удалить последний, но видимо не удаляет, следующий тест test Get который так же добавляет три элемента и второй удаляет и так же ошибка, и testAddGetNull который добавляет 1 затем null затем 2 и вызывает lis.get(1) но возвращается 1.

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;

public class SingleLinkedListImpl implements List {

private Node head;

private static class Node {
    Object data;
    Node next;

    Node(Object data) {
        this.data = data;
    }

    Node(Object data, Node next) {
        this.data = data;
        this.next = next;
    }

    @Override
    public String toString() {
        return "[" + data + ']';
    }
}

public SingleLinkedListImpl() {
    head = new Node( null);
}

@Override
public void clear() {
    head.next = null;
}
private int getSize(){
    int count = 0;
    Node current = head.next;
    while (current != null) {
        count++;
        current = current.next;
    }
    return count;
}

@Override
public int size() {
    return getSize();
}

/**
 * Inserts the specified element at the <b>front</b> of this list
 *
 * @param el the element to add
 * @return {@code true} if this list was changed {@code false} otherwise
 */
@Override
public boolean add(Object el) {
    if (el == null) {
        throw new NullPointerException();
    }

    if (head.next == null && head.data == null) {
        head.next = new Node(el);
    } else {
        Node newNode = new Node(el);
        newNode.next = head.next;
        head.next = newNode;
    }

    return true;
}

@Override
public Optional<Object> remove(Object el) {
    Node curr = head.next;
    Node prev = head;
    while(curr != null){
        if(Objects.equals(curr.data, el)){
            prev.next = curr.next;
            curr.next = null;
            return Optional.of(curr.data);
        }
        prev = curr;
        curr = curr.next;
    }
    return Optional.empty();
}

@Override
public Object get(int index) {
    if(index <= 0 || index >= size()){
        throw new IndexOutOfBoundsException();
    }

    Node curr = head.next;
    for (int i = 0; i < index; i++) {
        curr = curr.next;
    }
    return curr.data;
}

/**
 * Makes a string representation of this list.
 * The elements ordering must be coordinated with the
 * {@code Iterator} of this list.
 *
 * @return a string representation of this list.
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder("[");
    Node curr = head.next;
    while(curr != null){
        sb.append(curr.data);
        curr = curr.next;
        if(curr != null){
            sb.append(", ");
        }
    }
    sb.append("]");
    return sb.toString();
}

/**
 * Returns an iterator over elements.
 * Iterator must implement the remove method.
 * <pre>
 * list.add("A");
 * list.add("B");
 * list.add("C");
 * for(Object obj : list) { System.out.print(obj) } // prints: CBA
 * </pre>
 *
 * @return an iterator
 */
@Override
public Iterator<Object> iterator() {
    return new Iterator<Object>() {
        private Node curr = head.next;
        private Node prev = head;
        private boolean canRemove = false;

        @Override
        public boolean hasNext() {
            return curr != null;
        }

        @Override
        public Object next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            Object data = curr.data;
            prev = curr;
            curr = curr.next;
            canRemove = true;
            return data;
        }

        @Override
        public void remove() {
            if (!canRemove) {
                throw new IllegalStateException();
            }
            prev.next = curr;
            curr = curr.next;
            canRemove = false;
        }
    };
}

тесты которые не проходят

@Test
    void testAddGetNull() {
        List list = new SingleLinkedListImpl();
        list.add(1);
        assertThrows(NullPointerException.class, () -> list.add(null),
                "The list must not accept null values");
        list.add(2);
        assertIterableEquals(asList(2, 1), list,
                "The list must not accept null values");
        assertEquals(2, list.get(1));
    }  



@Test
    void testIterator7() {
        // test correct position of element deletion if they are not unique
        List a = new SingleLinkedListImpl();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(2);
        Iterator<Object> it = a.iterator();
        assertEquals(2, it.next());
        assertEquals(3, it.next());
        assertEquals(2, it.next());
        it.remove();
        assertIterableEquals(asList(2,3,1), a,
                "Must remove last returned element. Actual: " + a);
    }

@Test
    void testGet() {
        List list = new SingleLinkedListImpl();
        list.add(1);
        list.add(2);
        list.add(3);
        assertEquals(3, list.get(2));
        assertEquals(Optional.of(1), list.remove(1));
        assertEquals( 2, list.get(0));
    }

Ответы (0 шт):