简体   繁体   中英

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()

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.

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.)

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)

getPrevious() (or getNext() ) on the list itself would mean the list holds an iteration position, which is not usually the case.

I guess that you have information about what current element has index (if not use find() method), so basically you can call:

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

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.

The concept of a linked list is not specific to 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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