开发者

doubly-linked list

Hi I want to know that how can I copy my objects from an arrayList to a doubly linked list? also my DNode constructor is :

    public DNode(Object element, DNode prev, DNode next) {
    this.element = element;
    this.next = next;
    this.prev = prev;
}

i.e. when I write such a code my program doesn't work :

  DNode node = new DNode(pointList.get(0),null, null);

        for (int i = 1; i <开发者_StackOverflow; pointList.size(); i++) {
        DNode dNode = new DNode(pointList.get(i), node, null);
        dList.addLast(dNode);
        }

also i have written doubly linked list which has addAfter and addBefore methods and also much more.


java.util.LinkedList is a doubly-linked list.

All of the operations perform as could be expected for a doubly-linked list.

You can create it by passing the array list as constructor argument:

List linkedList = new LinkedList(arrayList);

Update: The java.util.LinkedList has add(index, element) which, combined with indexOf(..) should cover the addBefore and addAfter methods. You can extend LinkedList to add these convenient methods if you like.


Assuming the element at the end of the linked list has a 'next' property of 0:

ArrayList arrayList = new ArrayList();
int next = currentElement.next;
while(next != 0) {
    arrayList.add(currentElement);
    next = currentElement.next;
}

You can also use java.util.LinkedList as that is an in-built representation of a doubly-linked list. Using this type means you can pass the linked list into the constructor of an ArrayList

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜