简体   繁体   English

如何在Java中的单链表中实现getPrevious方法

[英]How do I implement getPrevious method in singly linked list in Java

I want to implement: 我要实施:

public Object getPrevious(); and reset() method.

* It should return Using the SAME internally maintained pointer as getNext(), * return the contents of the node in the list immediately preceding the item last returned * by either getNext() or getPrevious() *它应该使用SAME内部维护的指针作为getNext()返回,*通过getNext()或getPrevious()返回紧接最后返回的项目之前列表中节点的内容。

and reset will reset the list so that getPrevious() and getNext() start from the beginning that is it should behave as if we never called those methods. 然后reset将重置列表,以便getPrevious()和getNext()从头开始,它的行为就像我们从未调用过这些方法一样。

In single linked list. 在单个链接列表中。 I have already implemented: 我已经实现了:

public int length();
public Object first();
public Object last();
public boolean lookup(Object obj);
public Object get(int n);
public void add(Object o);
public int find(Object obj);
public void delete(Object obj);
public void delete(int n)

The only way of getting to the previous node would be to walk from the head until you find a node whose "next" node is the one whose previous node you're trying to find. 到达上一个节点的唯一方法是从头开始直到找到一个节点,该节点的“下一个”节点是您要查找的上一个节点。 It's inefficient, which is why doubly-linked lists are often preferred to singly-linked ones. 它的效率很低,这就是为什么双向链接列表通常比单链接列表更受欢迎的原因。 (The exception being for lists in functional programming languages, which are generally immutable... you can "append" to an immutable singly-linked list efficiently, by remembering the "head" list and the new tail value. That doesn't work if the list has to be doubly-linked though.) (例外是功能编程语言中的列表,这些列表通常是不可变的……您可以通过记住“ head”列表和新的tail值来有效地“追加”到不可变的单链接列表。这是行不通的如果该列表必须进行双向链接。)

Most often you can have getNext() on a Node , where you just call node.getNext().getValue() (if you have a Node with two fields - Object value and Node next . Well, you can have Node previous , so that you traverse the list in reverse order (and you'll have to store the tail rather rather than the head) 通常,您可以在Node上拥有getNext() ,只需调用node.getNext().getValue() (如果您的Node带有两个字段node.getNext().getValue() Object valueNode next ,那么您可以将Node previous ,所以您以相反的顺序遍历列表(并且必须存储尾部而不是头部)

getPrevious() (or getNext() ) on the list itself would mean the list holds an iteration position, which is not usually the case. 列表本身上的getPrevious() (或getNext() )将意味着列表具有迭代位置,通常情况并非如此。

I guess that you have information about what current element has index (if not use find() method), so basically you can call: 我想您已经了解了当前元素具有索引的信息(如果不使用find()方法),那么基本上可以调用:

public Object getPrevious() {
    Object result = null;
    if (currentNumber > 0) {
        result = get(currentIndex - 1);
    }
    return result;
}

Another solution can be implement findPrevious(): it'll be iterate from head to your current node and remember what previous node is, then return it 另一个解决方案可以实现findPrevious():它将从头到当前节点进行迭代,并记住先前的节点是什么,然后将其返回

Is it possible to do this while still maintaining a singly linked list? 在保持单个链接列表的同时还能做到这一点吗? Yes, but you should stop and think about it before you do. 是的,但是您应该停下来仔细考虑。 If this is for a school assignment or something; 如果这是用于学校作业或其他事情; then chances are this question was rhetorical, and meant to make you think about the nature of a linked list. 那么这个问题很可能是夸张的,是要让您考虑链接列表的性质。 So unless your school assignment (again - I'm just assuming this is for school) specifically says "implement A getPrevious method for a singly-linked list" I would not do it. 因此,除非您的学校作业(再次-我只是假设这是给学校的)明确说“为单链接列表实现getPrevious方法”,否则我不会这样做。

The concept of a linked list is not specific to Java. 链表的概念并不特定于Java。 It refers to a commonly accepted definition of how to best represent a collection of data such that SINGLE direction iteration (hence the name SINGLY linked list) is very fast. 它指的是关于如何最好地表示数据集合的公认定义,以使SINGLE方向迭代(因此称为SINGLY链表)非常快。 In other words, say you had a to-do list; 换句话说,假设您有一个待办事项清单; and you know that you will ALWAYS go through your to-do list IN ORDER NO MATTER WHAT, and all of the tasks on your list were independent of each other; 并且您知道您将始终无所事事地浏览待办事项清单,并且清单上的所有任务都是相互独立的; so you NEVER have to know what you have to do 2 or 3 tasks down the road. 因此您永远不必知道接下来要做的2或3个任务。 In this situation, you would use a singly linked list. 在这种情况下,您将使用一个单链表。

The point here is that you might be asking the wrong question. 这里的重点是您可能会问错问题。 Instead of asking; 而不是问; "how do I get the previous element in my singly-linked list" you should be asking "is a linked list really the most useful data structure here?" “您应该问“链表真的是这里最有用的数据结构吗?”,“如何获取单链表中的上一个元素”? Once you've thought about that I would recommend looking into a doubly-linked list as @Bart Kiers commented. 一旦您想到了这一点,我建议您按照@Bart Kiers的评论查找双向链接的列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM