【问题标题】:Why my function is not returning a stack (self made stack class) when data is storing in stack? [closed]当数据存储在堆栈中时,为什么我的函数没有返回堆栈(自制堆栈类)? [关闭]
【发布时间】:2021-04-15 12:34:57
【问题描述】:

我正在尝试首先将数据保存在 java 中自制的堆栈数据结构对象中。但问题是,当我想返回该堆栈以在另一个可以使用该堆栈的函数中重用时,它会在那里返回一个空堆栈。但是,在上一行中,我打印了所有堆栈元素并且它不是空的。代码如下:

public Stack allLeafNodes(TreeNode root)
{
    Stack stack= new Stack();
    if (root == null)
        return null;
    if (root.getLeft() == null &&
            root.getRight() == null)
    {
        stack.push(root.getData());
    }
    if (root.getLeft() != null)
        allLeafNodes(root.getLeft());
    if (root.getRight() != null)
        allLeafNodes(root.getRight());
    else{
        stack.print(); // at this call all elements are getting printed
        return stack; //but when it returns, the stack is empty
    }
    return stack;
}

【问题讨论】:

  • 在一张纸上画出你的“堆栈”并使用指针(钢笔、铅笔)跟踪执行以指示当前位置并与代码进行比较。 (请注意,您的代码看起来像 ,而不是堆栈。)
  • 在修复 print 方法后变得相关的另一个注释:您可能忘记将两个 allLeafNodes 调用的结果添加到您的 stack。在当前的实现中,这些结果会被忽略。
  • 你需要包含调用allLeafNodes的代码。

标签: java function stack dsa


【解决方案1】:

我认为在您的print() 方法实现中您使用pop() 方法。因为它的这种行为是正常的。来自 javadoc pop()

Removes the object at the top of this stack and returns that object as the value of this function.

无论如何,您的方法print() 清除了您的堆栈。

【讨论】:

    【解决方案2】:

    您似乎使用了一些自定义类Stack(不是java.util.Stack)和方法print的自定义实现,它可能使用pop()操作,从而从堆栈中删除所有元素。

    因此,方法print()需要修改;可能值得重写Stack 的方法toString() 以确保堆栈内容未被修改并将其用于调试打印。


    更新

    另一个问题是递归方法allLeafNodes 的实现,其中堆栈的结果实例总是重新创建,而它需要在左右节点的递归调用中作为累加器传递:

    public static Stack allLeafNodes(TreeNode root) {
        return allLeafNodes(root, new Stack());
    }
    
    public static Stack allLeafNodes(TreeNode root, Stack stack) {
    
        if (root == null)
            return stack;
            
        if (root.left == null && root.right == null) {
            stack.push(root.data);
        }
        if (root.left != null)
            allLeafNodes(root.left, stack);
        if (root.right != null)
            allLeafNodes(root.right, stack);
        
        //    stack.print(); // at this call all elements are getting printed
        System.out.println(stack);
        return stack;
    }
    

    否则,Stack 实现需要有一个类似于Collections::addAll 的方法,当从递归调用返回时,必须使用该方法添加“内部”叶节点的内容:

    public static Stack allLeafNodes(TreeNode root) {
    
        if (root == null)
            return null;
        
        Stack stack = new Stack();
        if (root.left == null && root.right == null) {
            stack.push(root.data);
        }
        if (root.left != null) {
            Stack left = allLeafNodes(root.left);
            if (null != left)
                stack.addAll(left);
        }
        if (root.right != null) {
            Stack right = allLeafNodes(root.right);
            if (null != right)
                stack.addAll(right);
        }
        
        //    stack.print(); // at this call all elements are getting printed
        
        System.out.println(stack);
        return stack;
    }
    

    【讨论】:

    • 是的,我正在使用我自己定制的 Stack 类...但是当它打印所有数据时为什么它返回一个空的(在打印后的下一行)我尝试删除打印功能也是如此,但问题仍然存在
    • @Techistanpro,请查看更新后的答案
    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2021-03-05
    • 2013-09-30
    • 2019-05-23
    相关资源
    最近更新 更多