使用LinkedList实现ArrayList

时间:2014-05-31 23:54:49

标签: java arraylist linked-list internal doubly-linked-list

我需要使用内部LinkedList来实现Queue和ArrayList。我创建了我的DoublyLinkedList类,并且能够毫无问题地将它实现到我的队列中。我遇到的问题是,要添加或删除ArrayList,添加/删除方法接受索引和对象/元素的整数。我的DoublyLinkedList类中的所有方法都包含元素和/或节点。

我的问题是,当我的DLL类没有接受任何int值时,如何在我的ArrayList中实现我的DoublyLinkedList方法。

我希望能够使用索引添加或删除节点,但我不能。实际上,我想要像list.addAfter(I)这样的东西,而不是我是一个整数。

注意:此赋值的目标是实现ADT,因此我无法修改ArrayList ADT的方法签名。

DoublyLinedList Class

public class DoublyLinkedList<E> {

private Node<E> head;

private Node<E> tail;

private int size;

public DoublyLinkedList() {

    this.head = new Node<E>(null, null, null);
    this.tail = new Node<E>(null, null, null);
    this.size = 0;

    head.setNext(tail);
    tail.setPrev(head);

}

public int size() {

    return size;

}

public boolean isEmpty() {

    return size == 0;
}

public Node<E> getPrev(Node<E> n) {

    return n.getPrev();

}

public Node<E> getNext(Node<E> n) {

    return n.getNext();

}

public Node<E> getFirst() {

    return head.getNext();

}

public Node<E> getLast() {

    return tail.getPrev();

}

public E remove(Node<E> c) {
    Node<E> a = c.getPrev();
    Node<E> b = c.getNext();
    b.setNext(a);
    a.setPrev(b);
    c.setNext(null);
    c.setPrev(null);
    size--;
    return c.getElement();
}

public E removeFirst() {
    return remove(head.getNext()); // first element is beyond header
}

public E removeLast() {
    return remove(tail.getPrev());
}

public void addBefore(Node<E> node, E e) {

    Node<E> prev = getPrev(node);

    Node<E> n = new Node<E>(e, prev, node);

    node.setPrev(n);
    prev.setNext(n);

    size++;

}

public void addFirst(E e) {

    addAfter(head, e);

}

public void addLast(E e) {

    addBefore(tail, e);

}

public void addAfter(Node<E> node, E e) {

    Node<E> next = getNext(node);

    Node<E> n = new Node<E>(e, node, next);
    node.setNext(n);
    next.setPrev(n);
    size++;

}

}

LArrayList类(我的Arraylist实现)

public class LArrayList implements List {

private DoublyLinkedList list;

private int size;

public LArrayList() {

    this.list = new DoublyLinkedList();
    this.size = 0;

}

public int size() {
    return size;
}

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

public void add(int I, Object e) throws IndexOutOfBoundsException {

    if (isEmpty()) {

        list.addFirst(e);

    }

    // HERE IS MY CONCERN. THESE FOUR METHODS ALL TAKE IN INT VALUES WHILE
    // NON OF MY DLL METHODS DO!

}

public Object get(int i) throws IndexOutOfBoundsException {
    return null;
}

public Object remove(int i) throws IndexOutOfBoundsException {
    return null;
}

public Object set(int I, Object e) throws IndexOutOfBoundsException {
    return null;
}

}

2 个答案:

答案 0 :(得分:0)

public Object get(int i) throws IndexOutOfBoundsException {
    if(list.size()<=i) throw new IndexOutOfBoundsException();
    Node current = list.getFirst();
    for(int x = 0; x<=i; x++){
       if(x == i) return current.getElement();//Change this behaviour for remove and set
       current = current.getNext();
    }
}

答案 1 :(得分:0)

这似乎是一件相当容易的事情 - 只需使用LinkedList公开的API并为其添加一些逻辑。这是你缺少的一点

if (list.size() < I) {
    throw new IndexOutOfBoundsException()
}

//get a starting point
Node node = list.getFirst();

//loop until you get to the specified position
while(I-- > 0) {
    node = list.getNext(node);
} 

//now node points at the node in position I - insert the new
//node before it to comply with the List interface
list.addBefore(node, e);

this.size++;

我必须注意你的LinkedList实现可以改进 - 首先,getPrev() getNext() addBefore()addAfter()应该是静态的,因为你不应该&# 39; t必须使用LinkedList实例来调用它们。但是,如果这些方法实际上是Node中的方法会更好,因为这样可以更容易地遍历和使用LinkedList。如果方法在Node中,上面的代码就是这样的:

if (list.size() < I) {
    throw new IndexOutOfBoundsException()
}

//get a starting point
Node node = list.getFirst();

//loop until you get to the specified position
while(I-- > 0) {
    node = node.getNext();
} 

//now node points at the node in position I - insert the new
//node before it to comply with the List interface
node.addBefore(e);

this.size++;

你根本不需要列表 - 当然你不需要将额外的参数传递给某些功能。您仍然可以在链接列表中保留(希望是静态的)方法来执行相同的操作,但它们只是方法的Node实现的代理,例如:

public static void addAfter(Node<E> node, E e) {
    node.addAfter(e);
}

我不确定你是否需要在LinkedList中使用这些方法,但如果你愿意的话,他们当然可以用于#34;向后遵守&#34;。

编辑忘记提及 - 第一位代码是add()的实现,我相信你可以解决剩下的问题,因为他们会做同样的事情。 / p>