简体   繁体   English

如何找到堆栈中的最大值? (爪哇)

[英]How is it possible to find the highest value in a stack? (java)

I am preparing for an exam on stacks and I'm practicing on various methods regarding stack.我正在准备堆栈考试,并且正在练习有关堆栈的各种方法。 I tried to implement a method that return the highest value in a stack but I'm a little bit confused.我试图实现一个返回堆栈中最大值的方法,但我有点困惑。

I should remove the stack top of this and push it in a new stack s1 , then compare this.stackTop with s1.stackTop .我应该删除它的堆栈顶部并将this推入新堆栈s1 ,然后将this.stackTops1.stackTop进行比较。

This is my code:这是我的代码:

        Stack s1 = new Stack();
        
        if(this.isEmpty()) {
            System.out.println("the stack is empty ");
            return NOT_FOUND;
        }
        
        while(!this.isEmpty() && this.stackTop() > s1.stackTop()) {
            s1.push(this.stackTop());
            this.pop(); 
        }
        
        while(!s1.isEmpty()) {
            this.push(s1.stackTop());
            s1.pop();
        }
    }

I'm just confused and don't know how to proceed so any help would be appreciated.我只是很困惑,不知道如何进行,所以任何帮助将不胜感激。

If the stack isn't sorted (which it normally isn't, since it would be very inefficient to sort stacks) you need to run through the entire stack.如果堆栈未排序(通常不是,因为对堆栈进行排序会非常低效),您需要遍历整个堆栈。 So you would have to run through every element until the stack is empty, just like when finding the largest number from an array.因此,您必须遍历每个元素,直到堆栈为空,就像从数组中查找最大数一样。 On a stack though you can't access elements in the middle of the stack, so you have to use s1 to track and "shift" the elements from the main stack to be able to compare them all.在堆栈上,虽然您无法访问堆栈中间的元素,但您必须使用s1来跟踪和“移动”主堆栈中的元素,以便能够比较它们。 After you've found the largest value you need to return all the elements in s1 back to the main stack.找到最大值后,您需要将s1中的所有元素返回到主堆栈。

Imagine you have a box.想象一下,你有一个盒子。 In this box there are books which are all the same size and perfectly fit the box.在这个盒子里有书,大小都一样,完全适合盒子。 This means you can't just take out any book from the box.这意味着您不能只从盒子中取出任何书。 So to find the book with the most pages you need to take out every book and compare it to the current largest book you have found.因此,要找到页数最多的书,您需要取出每本书并将其与您找到的当前最大的书进行比较。 And since you want to put the books back in order you put them in a second box.既然你想把书按顺序放回去,你就把它们放在第二个盒子里。 Afterwards when you found the longest book take each book out of the temporary box and put it back into the original box.之后,当你找到最长的一本书时,将每本书从临时盒子中取出并放回原来的盒子里。

// Create your temporary stack
Stack s1 = new Stack();
// Initialize the first value with the minimal value
int largest = Integer.MIN_VALUE;

if (this.isEmpty()) {
    System.out.println("the stack is empty ");
    return NOT_FOUND;
}

// Loop through the main stack until it's empty
while (!this.isEmpty()) {
    // Take out the top element
    int current = this.pop();
    // Compare it to the largest that you have found until now
    if (current > largest) {
        // If it's larger remember it
        largest = current;
    }

    // And put it into the temporary stack
    s1.push(current);
}

// Loop through the temporary stack until it's empty
while (!s1.isEmpty()) {
    // Take the top element from the temporary stack and put it onto the main one
    this.push(s1.pop());
}

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

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