从尾到头遍历 LinkedList [英] Traversing a LinkedList tail to head

查看:31
本文介绍了从尾到头遍历 LinkedList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历这个自定义的链表实现并显示其内容:

I need to traverse through this custom linked list implementation and display its contents:

  • 从头到尾,
  • 然后从尾部到头部.

我能够使用 for 循环轻松地从头到尾显示列表内容:

I was able to display the lists contents from head to tail pretty easily with a for loop:

        for (AccountRecordSerializable account : list) {
            System.out.println(account); 
        }

一切正常.现在我正试图扭转这种局面.在提供的 LinkedList 类中使用,其中也有一个 LinkedListIterator 类.迭代器类具有诸如 hasNext()hasPrevious() 之类的方法,我知道它们可以用来这样做,但我不太确定如何使用该迭代器通过我的 LinkedList 这样做.

and everything works fine. Now I am trying to reverse that. In provided LinkedList class to use which has a LinkedListIterator class inside it as well. The iterator class has methods such as hasNext(), hasPrevious() which I know can be used to do so, but I'm not quite sure how to use that iterator through my LinkedList to do so.

有没有像我以前所做的那样更简单的方法来扭转这种情况?或者我将如何使用 Iterator 类遍历我的列表以执行任务?

Is there a simpler way as I had done before to reverse this? Or how would I use the Iterator class to iterate through my list so that it performs the task?

如果这没有任何意义,我深表歉意……如果您需要澄清,请告诉我……谢谢.

I apologize if this doesn't make any sense... let me know if you need clarifications..Thanks.

推荐答案

Java LinkedList 实现接口 Deque 提供方法 降序迭代器.

The Java LinkedList implements interface Deque that provides method descendingIterator.

以相反的顺序返回此双端队列中元素的迭代器.元素将按照从尾(尾)到首(头)的顺序返回.

Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).

我对您的建议是在您的类中实现该接口,并获得反向迭代器.

My advise for you is to implement that interfaces in your class, and the obtain reversed iterator.

链表 是具有一些属性的数据结构,您应该使用这些属性来获取实现.链表的典型构造是一个元素指向下一个元素.在您的情况下,您有支持双链表的实现.

The linked list is data structure with some properties that you should use to get the implementation. The typical construction of linked list is that an element points to next one. In your case you have implementation that supports double linked list.

private int size = 0; // size can never be < 0
private DLNode<E> head;
private DLNode<E> tail;

在您的代码中,您有 DLNode 代表双链接节点.这意味着您可以使用 hasNex()head 移动到 tail 并使用 hasPrevious() 从尾移动到头代码>.

In your code you have DLNode that stand for Double Linked Node. This means that you can move from head to tail by using hasNex() and from tail to head using hasPrevious().

在您的类中,您可以使用此方法获得 LinkedListIterator 类:

In your class you have the class LinkedListIterator, that you can obtain with this method:

 public ListIterator<E> listIterator(int index) {
    if ((index < 0) || (index > size)) {
        throw new IndexOutOfBoundsException("index " + index+ " is out of range: 0 to " + size);
    }
    return new LinkedListIterator<E>(index);
}

所以要打印你的元素,你可以这样做.

So to print your elements you could do it like this.

 public <T> void printLinkedListFromHead(LinkedList<T> list) {

   for(ListIterator<T> iterator = list.listIterator(0); iterator.hasNext();) {
       System.out.println(iterator.next());
   }

 }

您还应该为您的代码创建一个单独的类,您将在其中放置上下文中不属于链表实现的代码.方法 readObjectswriteObjects 不属于类.和主要一样.

You should also create a separate class for your code, where you will put your code that contextually does not belong to the linked list implementation. The method readObjectsand writeObjects do not belong to class. same as main.

如果你有标准的 Java LinkedList,你可以这样写:

If you would have standard Java LinkedList you could wrote something like this:

public <T> reversePrint(Deque deque) {

 for (Iterator<T> iterator = deque.descendingIterator(); iterator .hasNext();){
      System.out.println(iterator .next());
    }
}

为了缩小迭代器promise for循环的范围而不是while.

To narrow the scope of iterator promote for loop than while.

这篇关于从尾到头遍历 LinkedList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆