-1
public class SinglyLinkedList implements Lista {
private final Element _headAndTail = new Element(null);
private int _size;

public SinglyLinkedList() {
    clear(); 
}

private static final class Element {

    private Object _value;
    private Element _next;

    public Element(Object value) {
        setValue(value);
    }

    public void setValue(Object value) {
        _value = value;
    }

    public Object getValue() {
        return _value;
    }

    public Element getNext() {
        return _next;
    }

    public void setNext(Element next) {
        assert next != null : "Wskaźnik na element następny nie może być pusty";
        _next = next;
    }

    public void attachBefore(Element e) {
        setNext(e);
        e.setNext(this);
    }
}

public void insert(int index, Object value)
        throws IndexOutOfBoundsException {
    if (index < 0 || index > _size)
        throw new IndexOutOfBoundsException();
    Element element = new Element(value);
    element.attachBefore(getElement(index));
    ++_size;
}

private Element getElement(int index) {
    return getElementForwards(index);
}

private Element getElementForwards(int index) {

    Element element = _headAndTail.getNext();
    for (int i = index; i > 0; --i)
        element = element.getNext();
    return element;
}

private void checkOutOfBounds(int index) throws IndexOutOfBoundsException {
    if (index < 0 || index >= size())
        throw new IndexOutOfBoundsException();
}

public void add(Object value) {
    insert(size(), value);
}

public int size() {
    return _size;
}

public Object get(int index) throws IndexOutOfBoundsException {
    checkOutOfBounds(index);
    return getElement(index).getValue();
}

public Object set(int index, Object value) throws IndexOutOfBoundsException {
    checkOutOfBounds(index);
    Element element = getElement(index);
    Object oldValue = element.getValue();
    element.setValue(value);
    return oldValue;
}

public Object delete(int index) throws IndexOutOfBoundsException {
    checkOutOfBounds(index);
    Element element = getElement(index);
    --_size;
    return element.getValue();
}

public boolean delete(Object value) {
    Element e = _headAndTail.getNext();
    while (e != _headAndTail && !value.equals(e.getValue()))
        e = e.getNext();
    if (e != _headAndTail) {
        --_size;
        return true;
    }
    else
        return false;
}

public boolean contains(Object value) {
    return indexOf(value) != -1;
}

public void clear() {
    _headAndTail.setNext(_headAndTail);
    _size = 0;
}

public int indexOf(Object value) {
    int index = 0;
    Element e = _headAndTail.getNext();
    while (e != _headAndTail && !value.equals(e.getValue())) {
        e = e.getNext();
        ++index;
    }
    return e != _headAndTail ? index : -1;
}

public boolean isEmpty() {
    return _size == 0;
}

public Iterator iterator() {
    return new ValueIterator();
}

private final class ValueIterator implements Iterator {

    private Element _current = _headAndTail;

    public void first() {
        _current = _headAndTail.getNext();
    }

    public void last() {
    }

    public boolean isDone() {
        return _current == _headAndTail;
    }

    public void next() {
        _current = _current.getNext();
    }

    public void previous() {
    }

    public Object current() throws IndexOutOfBoundsException {
        if (isDone())
            throw new IndexOutOfBoundsException();
        return _current.getValue();
    }
}

}

这是我的代码,它的编译没有任何问题,我已经制作了这样的双向链表,它没有任何问题,当我将它更改为单链表时,如果有人可以看一下,它不会将对象添加到列表中会很棒 - 我认为问题是方法insert(int index, Object value),并且attachBefore(Element e)在 Element 类中。

4

1 回答 1

1

中存在概念问题Element.attachBefore(e)。基本上不可能为单链表实现该方法。相反,您必须Element在要插入的位置之前找到,并在其后插入新Element 。(并处理您在列表开头插入的特殊情况。)

我会让你处理细节...

但纳伦德拉的评论是正确的。如果您无法弄清楚当前版本的代码在做什么,请使用 IDE 的 Java 调试器运行它,并单步调用给您带来麻烦的方法。

于 2013-03-07T14:49:18.743 回答