【问题标题】:Stack in data structure数据结构中的堆栈
【发布时间】:2021-05-29 11:27:29
【问题描述】:

我是数据结构的新手。我尝试输入 5 个字符并使用 pop() 函数将其反转。但数组中最后一个索引的值显示为空。谁能解释一下原因?

堆栈类:

package stack;

public class Stack {
    
    int top = 0;
    String stack[] = new String[5];
    
    public void push(String val) {
        
        if(top >= 4) {
            
            System.out.println("Overflow Condition");
            
        }
        
        else {
            
            stack[top] = val;
        
            System.out.println("new value for index"+top+" " +"is"+" "+stack[top]);
            
            top++;
            
        }
        
    }
    
    public void pop() {
        
        String output ;
        
        if(top == -1) {
            
            System.out.println("Underflow Condition");
        }
        
        else {
            
            
            for(int i= top; i >= 0; i--) {
            
            output = stack[top];
            
            System.out.println("Removed Index:"+" "+(top+1)+"is"+ output);
            top--;
            
            }
            
        }
        
    }
    
    public void peek() {
        
        String output;
        
        if(top >= 4) {
            
            System.out.println("Overflow Condition");
        }
        
        else {
            
            output = stack[top];
            
        }
        
    }   
    
}

主程序:

package stack;

import java.util.Scanner;

public class StackHome {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner inp = new Scanner(System.in);
        
        Stack obj = new Stack();
        
        String arr[] = new String[5];
        
        for(int i=0; i<5; i++) {
        
            System.out.println("Value to add for stack");
            String value = inp.next();
            
            arr[i] = value;
            
            obj.push(arr[i]);
        }
        
        obj.pop();
        //obj.peek();
        
    }

}

输入

为堆栈添加的值
一个
index0 的新值是 a
为堆栈添加的值
b
index1 的新值为 b
为堆栈添加的值
c
index2 的新值为 c
为堆栈添加的值
d
index3 的新值是 d
为堆栈添加的值
e

输出

溢出条件
删除的索引:4is null
删除索引:3is d
删除索引:2is c
删除索引:1is b 删除的索引:0 是一个

【问题讨论】:

  • 为什么要删除pop()-方法中的多个元素?
  • 你好 @dan1st 我只是尝试使用 pop() 方法删除我使用 push() 方法添加到堆栈的所有值。
  • pop() 用于移除栈顶元素。
  • @dan1st 是的。但是在 pop() 函数中,我使用 top-- 减少了 top 。有错吗??
  • 是的,但为什么在 for 循环中?除此之外,为什么在pop方法中检查-1而不是0?

标签: java data-structures stack


【解决方案1】:

我想指出一些更改以简化您的代码。

  1. 您不需要在main() 方法中创建新的array,因为您已经在Stack 类中创建了array(用作stack)。

    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        Stack obj = new Stack();
        //this loop is for pushing elements into the stack
        for (int i = 0; i < 5; i++) {
            System.out.println("Value to add for stack");
            String value = inp.next();
            //you only need to call the push() method to push the element into the array created in the Stack class.
            obj.push(value);
        }
        //this loop is for popping the elements from the stack
        for (int i = 0; i < 5; i++) {
            System.out.println("Value to remove from stack");
            //you just need to call the pop() method to pop an element from the stack 
            obj.pop();
        }
        inp.close();
    }
    
  2. 您的pop() 方法不需要for 循环,因为它唯一的工作就是删除一个元素。您可以在main() 中运行一个循环来逐个弹出元素,如上所示。所以你的pop()方法可以修改如下:

    public String pop() {
        if (top == 0) {
            System.out.println("Underflow Condition");
        }
        else { //no need of for loop here.
                top--;
                String output = stack[top];
                System.out.println("Removed Index:" + " " + (top + 1) + "is" + output);
                return output;
        }
        return null;
    }
    

注意:一旦所有元素都被推入stacktop 的值将为 5,即数组的length,因此您需要减少 @ 的值987654335@ 先用top--top 指向数组的最后一个索引,然后就可以返回弹出的元素了。

输入/输出如下图:

Value to add for stack
a b c d e
new value for index0 is a
Value to add for stack
new value for index1 is b
Value to add for stack
new value for index2 is c
Value to add for stack
new value for index3 is d
Value to add for stack
new value for index4 is e
Value to remove from stack
Removed Index: 5ise
Value to remove from stack
Removed Index: 4isd
Value to remove from stack
Removed Index: 3isc
Value to remove from stack
Removed Index: 2isb
Value to remove from stack
Removed Index: 1isa

我希望这能回答您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2015-10-21
    • 2021-10-02
    • 2020-07-08
    • 2023-03-22
    • 2018-04-27
    • 2022-01-22
    相关资源
    最近更新 更多