Java中的单链接列表实现

时间:2013-03-07 14:38:00

标签: java

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)和Element类中的attachBefore(Element e)

1 个答案:

答案 0 :(得分:1)

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

我会让你去处理细节...

但Narendra的评论很有见。如果您无法弄清楚当前版本的代码是做什么的,请使用IDE的Java调试器运行它,并通过调用给您带来麻烦的方法单步执行它。