Java迭代双向链表序列。

huangapple go评论68阅读模式
英文:

Java iterative over doubly linked list sequence

问题

我正在尝试编写一个迭代方法来获取火车序列的最后一节车厢。但是我在如何实现这个方法上卡住了。有人可以帮我指点一下方向吗?

方法:

/**
 * 找到连接到此车厢的车厢序列的最后一个车厢
 * 如果没有连接的车厢,则返回此车厢本身
 * @return  找到的车厢
 */
public Wagon getLastWagonAttached() {
    // TODO 提供一个迭代解决方案(无需递归)
    if (!hasNextWagon()){
        // 返回车厢
    }
    else {
        // 移动到下一个车厢
    }

    return null; // 返回最后一个车厢
}

附加信息,这是整个类:

public abstract class Wagon {

    // ...(其他代码在这里)

    /**
     * 找到连接到此车厢的车厢序列的最后一个车厢
     * 如果没有连接的车厢,则返回此车厢本身
     * @return  找到的车厢
     */
    public Wagon getLastWagonAttached() {
        // TODO 提供一个迭代解决方案(无需递归)
        if (!hasNextWagon()){
            // 返回车厢
        }
        else {
            // 移动到下一个车厢
        }

        return null; // 返回最后一个车厢
    }

    // ...(其他代码在这里)
}

以上是你提供的代码的翻译。如果你还有其他需要翻译的内容或问题,请告诉我。

英文:

Im trying to write a iterative method to get the last wagon of a train sequence.
But i'm getting stuck on how to do this. Can anyone help me in the right direction?

The method:

/**
     * finds the last wagon of the sequence of wagons attached to this wagon
     * if no wagons are attached return this wagon itselve
     * @return  the wagon found
     */
    public Wagon getLastWagonAttached() {
        // TODO provide an iterative solution (without recursion)
        if (!hasNextWagon()){
            // return the wagon
        }
        else {
            // move to next wagon
        }

        return null; // return the last wagon
    }

for extra this is the whole class:

public abstract class Wagon {

    protected int id;                 // some unique ID of a Wagon
    private Wagon nextWagon;        // another wagon that is appended at the tail of this wagon
                                    // a.k.a. the successor of this wagon in a sequence
                                    // set to null if no successor is connected
    private Wagon previousWagon;    // another wagon that is prepended at the front of this wagon
                                    // a.k.a. the predecessor of this wagon in a sequence
                                    // set to null if no predecessor is connected


    // representation invariant propositions:
    // tail-connection-invariant:   wagon.nextWagon == null or wagon == wagon.nextWagon.previousWagon
    // front-connection-invariant:  wagon.previousWagon == null or wagon = wagon.previousWagon.nextWagon

    public Wagon (int wagonId) {
        this.id = wagonId;
    }

    public int getId() {
        return id;
    }

    public Wagon getNextWagon() {
        return nextWagon;
    }

    public Wagon getPreviousWagon() {
        return previousWagon;
    }

    /**
     * @return  whether this wagon has a wagon appended at the tail
     */
    public boolean hasNextWagon() {
        return this.nextWagon != null;
    }

    /**
     * @return  whether this wagon has a wagon prepended at the front
     */
    public boolean hasPreviousWagon() {
        return this.previousWagon != null;
    }

    /**
     * finds the last wagon of the sequence of wagons attached to this wagon
     * if no wagons are attached return this wagon itselve
     * @return  the wagon found
     */
    public Wagon getLastWagonAttached() {
        // TODO provide an iterative solution (without recursion)
        if (!hasNextWagon()){
            // return the wagon
        }
        else {
            // move to next wagon
        }

        return null; // return the last wagon
    }

    /**
     * @return  the length of the sequence of wagons starting with this wagon
     *          return 1 if no wagons have been attached to this wagon.
     */
    public int getSequenceLength() {
        // TODO provide a recursive solution

        return 1;
    }

    /**
     * attaches this wagon at the tail of a given prevWagon.
     * @param newPreviousWagon
     * @throws RuntimeException if this wagon already has been appended to a wagon.
     * @throws RuntimeException if prevWagon already has got a wagon appended.
     */
    public void attachTo(Wagon newPreviousWagon) {
        // TODO verify the exceptions

        // TODO attach this wagon to its new predecessor (sustaining the invariant propositions).
    }

    /**
     * detaches this wagon from its previous wagons.
     * no action if this wagon has no previous wagon attached.
     */
    public void detachFromPrevious() {
        // TODO detach this wagon from its predecessors (sustaining the invariant propositions).

    }

    /**
     * detaches this wagon from its tail wagons.
     * no action if this wagon has no succeeding next wagon attached.
     */
    public void detachTail() {
        // TODO detach this wagon from its successors (sustaining the invariant propositions).

    }

    /**
     * attaches this wagon at the tail of a given newPreviousWagon.
     * if required, first detaches this wagon from its current predecessor
     * and/or detaches the newPreviousWagon from its current successor
     * @param newPreviousWagon
     */
    public void reAttachTo(Wagon newPreviousWagon) {
        // TODO detach any existing connections that will be rearranged

        // TODO attach this wagon to its new predecessor (sustaining the invariant propositions).

    }

    /**
     * Removes this wagon from the sequence that it is part of, if any.
     * Reconnect the subsequence of its predecessors with the subsequence of its successors, if any.
     */
    public void removeFromSequence() {
        // TODO

    }


    /**
     * reverses the order in the sequence of wagons from this Wagon until its final successor.
     * The reversed sequence is attached again to the predecessor of this Wagon, if any.
     * no action if this Wagon has no succeeding next wagon attached.
     * @return the new start Wagon of the reversed sequence (with is the former last Wagon of the original sequence)
     */
    public Wagon reverseSequence() {
        // TODO provide a recursive implementation

        return null;
    }

    // TODO
}

答案1

得分: 1

你需要一个循环,检查是否有下一节车厢,即 currentWagon.nextWagon != null。如果没有,返回 currentWagon。如果有,则将 nextWagon 分配为 currentWagon,并再次运行检查。

以下是一些伪代码

var currentWagon = startWagon;
while(currentWagon.nextWagon != null){
  currentWagon = currentWagon.nextWagon;
}
return currentWagon;
英文:

You need a loop that checks if there is a nextWagon, aka currentWagon.nextWagon != null. If there isn't return the currentWagon. If there is, assign the nextWagon as currentWagon and run the check again.

here is some pseudo code

var currentEntry = startEntry;
while(currentEntry.nextEntry != null){
  currentEntry = currentEntry.nextEntry;
}
return currentEntry;

huangapple
  • 本文由 发表于 2020年10月27日 02:34:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64543035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定