【问题标题】:recursive toString in Queue Linked List in JavaJava中队列链表中的递归toString
【发布时间】:2013-10-14 06:43:52
【问题描述】:

我需要为链表队列实现一个 toString() 递归方法。我知道我的 toString 方法在我上周做的一个链表实现上运行良好,所以我处理它的队列方面的方式有问题。

我的 QueueList 的 toString 方法:

public String toString() 
{ 

    if (front.info == null)
    {
        System.out.println("Error, queue is empty");
        return "";
    }
    if (front.link  == null) //base case: if this is last element in stack
    {

        return  (" \"" + front.info + "\" , ");
    } 
    else //normal recursive function
    {
        return  (" \"" + front.info + "\" , " + front.link.toString());

    }   

}

还有我的构造函数,比如 QueueList:

public class QueueNode 
{
    E info;
    QueueNode link;
}

private QueueNode front;//first element to be placed into queue
private QueueNode rear;//last element to be placed into queue
private int NoE;//counter for number of elements in queue
public QueueList() 
{ 
    front = null;
    rear = null;
    NoE = 0;
}

我试图用这个测试看看它发生了什么:

public boolean test() { 
    QueueList<String> q = new QueueList<String>();

    q.enqueue("The Godfather");
    q.enqueue("Casino");
    q.enqueue("Goodfellas");
    String r = q.toString();
    q.PrettyPrint();

输出

IN -> [ "The Godfather" , QueueList$QueueNode@a3901c6] -> OUT. 

我意识到这是因为我在 toString 方法的递归部分中说 front.link.toString(),但即使我将其更改为 front.link.info.toString(),我的输出也是

IN -> [ "The Godfather" , Casino] -> OUT. 

这可能与我的入队和出队方法有关,如下:

public void enqueue(E element) 
{ 

        QueueNode newNode = new QueueNode();//creates new Node to hold element
        newNode.info = element;//set info of new Node to element
        newNode.link = null;//make link null since it's at back of list
        if (rear == null)//checks if queue is empty
        {
            front = newNode;
        }
        else
        {
            rear.link = newNode;//sets second to last node's link to newNode
        }
        rear = newNode;//makes newNode the new last link
        NoE++;//increase counter

}
public E dequeue() throws InvalidOperationException 
{
    if (front == null)//sanitize code
    {
        throw new InvalidOperationException("There is nothing in the queue.");
    }
    E element = front.info;//creates an element file that takes the info in front of queue
    front = front.link;//makes second-to-front element new front
    if (front == null)//if this emptied the queue, make sure rear is also empty
    {
        rear = null;
    }
    NoE--;//reduce counter
    return element;
}

如果可以的话,请帮帮我。谢谢。

【问题讨论】:

    标签: java list recursion linked-list queue


    【解决方案1】:

    绝对没有必要让toString 递归,事实上这样做是不正确的。您的数据结构不是递归的(即树)而是线性的。

    如果您的列表包含 100 万个项目,您将很快耗尽堆栈空间(字面意思是 StackOverflow)。

    改用循环。

    编辑:如果您需要以递归方式执行此操作,那么问题是递归方法必须是QueueNode#toStringRecursive(),而不是Queue#toString()。方法Queue#toString() 分配一个缓冲区并将其提供给QueueNode 上的一个特殊toStringRecursive() 方法,该方法执行递归。 QueueNode#toString() 必须只对自己的节点内容负责。

    方法Queue#toString()

    public String toString()
    {
        StringBuilder buf = new StringBuilder();
        if (front == null)
            // queue is empty
        else
            front.toStringRecursive(buf);
        return buf.toString();
    }
    

    方法QueueNode#toStringRecursive()

    public void toStringRecursive(StringBuilder buf)
    {
        buf.append(this.toString());
        if (this.link != null)
            this.toStringRecursive(buf);
    }
    

    其中QueueNode.toString() 仅负责对一个节点(本身)进行字符串化。

    请注意,这是一种的方法。也可以将其写为Queue 上的递归方法,但不能称为toString()Queue#toString() 会设置初始条件,然后调用递归。

    【讨论】:

    • 我也有同样的感觉,就反复写,我去见教授说作业的另一个问题,她说我不递归写可能会丢分。感谢您的回复。
    • 对不起,你的教授弄错了。这不是递归问题,您的直觉是正确的。我已经更新了答案
    • 我 100% 同意你的看法。我的代码终于可以运行了,非常感谢您的帮助。
    • 如果您觉得我的回答有用,请点击投票计数器旁边的复选标记将其标记为“已接受”。
    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2018-03-16
    • 2010-09-26
    相关资源
    最近更新 更多