Java 使用LinkedList实现ArrayList
我需要通过使用内部LinkedList来实现队列和ArrayList。我创建了我的DoublyLinkedList类,并能够毫无问题地将其实现到队列中。我遇到的问题是,要在ArrayList中添加或删除,add/delete方法会为索引和对象/元素引入一个整数。我的DoublyLinkedList类中的所有方法都接受元素和/或节点 我的问题是,当我的DLL类中没有任何int值时,如何在ArrayList中实现我的DoublyLinkedList方法 我希望能够使用索引添加或删除节点,但我不能。实际上,我想要类似list.addAfter(I)的东西,而不是整数 注意:此任务的目标是实现ADT,因此我不能修改ArrayList ADT的方法签名 双ylinedlist类Java 使用LinkedList实现ArrayList,java,arraylist,linked-list,internal,doubly-linked-list,Java,Arraylist,Linked List,Internal,Doubly Linked List,我需要通过使用内部LinkedList来实现队列和ArrayList。我创建了我的DoublyLinkedList类,并能够毫无问题地将其实现到队列中。我遇到的问题是,要在ArrayList中添加或删除,add/delete方法会为索引和对象/元素引入一个整数。我的DoublyLinkedList类中的所有方法都接受元素和/或节点 我的问题是,当我的DLL类中没有任何int值时,如何在ArrayList中实现我的DoublyLinkedList方法 我希望能够使用索引添加或删除节点,但我不能。实
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++;
}
}
public Object get(int i)抛出IndexOutOfBoundsException{
如果(list.size()这似乎是一件相当容易的事情-只需使用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++;
您几乎不需要列表-当然,您不需要只向某些函数传递额外的参数。您仍然可以在链表中保留(希望是静态的)方法,它们可以做同样的事情,但它们只是这些方法的节点实现的代理,例如:
public static void addAfter(Node<E> node, E e) {
node.addAfter(e);
}
publicstaticvoidaddafter(节点,E){
node.addAfter(e);
}
我不确定您是否需要LinkedList中的这些方法,但如果您愿意,它们肯定可以用于“向后遵从性”
编辑忘了提及-第一段代码是add()的实现,我相信您可以解决其余的问题,因为他们也会这么做。谢谢您的帮助。我应该注意到缺少的“static”对于方法。我明白你在节点类中实现方法的观点。再次感谢!public Object get(int I)抛出IndexOutOfBoundsException{node current=list.getFirst();for(int x=0;上面的x是我获取(I)的方式)methodYep cool…这相当于我将返回当前;
更改为返回当前.getElement()
?我们可能还应该在循环之前抛出异常,只是为了性能(进行了更改)。
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++;
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++;
public static void addAfter(Node<E> node, E e) {
node.addAfter(e);
}