【问题标题】:How to implement a bounded stack in Java如何在 Java 中实现有界堆栈
【发布时间】:2013-12-18 12:10:14
【问题描述】:

我需要帮助的事情是:

  1. 使 isEmpty() 和 isFull() 方法返回答案以告诉我堆栈是否为空或堆栈是否已满的正确代码是什么

  2. 我需要将我正在使用的堆栈设置为五 (5) 个(堆栈可以容纳 5 个整数)

  3. 我需要在 push() 和 pop() 方法中添加异常,当堆栈已满时不允许使用 push,或者在堆栈为空时使用 pop


import java.util.ArrayList;
import java.util.List;

public class IntegerStack {

    private List<Integer> stack;

    public IntegerStack(int SIZE) {
        stack = new ArrayList<Integer>(SIZE);
    }

    /* method to push the stack */
    public void push(int i) {

        stack.add(0, i);
    }

    /* method to pop the stack */
    public int pop() {
        if (!stack.isEmpty()) {
            int i = stack.get(0);
            stack.remove(0);
            return i;
        } else {
            return -1;// Or any invalid value
        }
    }

    /* method to peek at the stack */
    public int peek() {
        if (!stack.isEmpty()) {
            return stack.get(0);
        } else {
            return -1;// Or any invalid value
        }
    }

    /* determine if the stack is empty */
    public boolean isEmpty() {
        stack.isEmpty();
    }

    /* determine if the stack is full */
    public boolean isFull() {
        stack.isFull();
    }

    /* determine the size of the stack */
    public int size() {
        if (stack.isEmpty())
            return 0;
        else
            return stack.size();
    }

}

【问题讨论】:

  • 你应该正确缩进你的代码,它真的很难阅读它
  • 一开始你错过了return关键字。返回堆栈.isEmpty();返回 stack.isFull();
  • 如果这是一个Stack,为什么推的时候还要提供一个位置?
  • 您也可以咨询the java.util.Stack类获取更多帮助。

标签: java stack


【解决方案1】:

在问之前你真的看了一下怎么做吗?好像你只是复制粘贴一个你没有得到的代码。

import java.util.ArrayList;
import java.util.List;

public class IntegerStack {

private int max_size;
private List<Integer> stack;

public IntegerStack(int size) {
    max_size = size;
    stack = new ArrayList<Integer>(size);
}

/* method to push the stack */
public void push(int i) {
        stack.add(0, i);
}

/* method to pop the stack */
public int pop() {
    if (!stack.isEmpty()) {
        int i = stack.get(0);
        stack.remove(0);
        return i;
    } else {
        return -1;// Or any invalid value
    }
}

/* method to peek at the stack */
public int peek() {
    if (!stack.isEmpty()) {
        return stack.get(0);
    } else {
        return -1;// Or any invalid value
    }
}

/* determine if the stack is empty */
public boolean isEmpty() {
    return stack.isEmpty();
}

/* determine if the stack is full */
public boolean isFull() {
    //go through all your stack and see if there are things not set to get if it's full.
}

/* determine the size of the stack */
public int size() {
    if (stack.isEmpty())
        return 0;
    else
        return stack.size();
}
}

当你创建你的对象时,你把 5 作为参数。您可能需要对代码进行其他更改;)

自己尝试,你会进步更多 :D

【讨论】:

  • 你的推送方式不对。并且列表接口没有提供isFull() 方法。
  • 我并没有说这是答案,而是找到答案的一种方式;)这家伙似乎并没有寻找答案,这就是为什么;)
  • 不是提供错误代码的理由(对于push 方法),即使您通过存储max_size 属性提供了有用的提示。
  • 我不想出错我很抱歉这个^^我把它改回原来的
【解决方案2】:

1。 正如 Clad 在 cmets 中提到的,您似乎只是缺少 isEmpty() 函数上的 return 关键字:

/* determine if the stack is empty */
public boolean isEmpty() {
    return stack.isEmpty();
}

但是,对于 isFull() 函数,List 类上没有 isFull() 函数,您必须自己实现它。您必须跟踪通过构造函数传入的整数,以便将其与存储在对象中的列表的大小进行比较

2。 您是否应该允许此对象的用户指定他们想要的任何 SIZE,还是应该始终有 5 个整数限制?

3。 您可能想阅读How to Throw Exceptions上的 Java 文档的这一部分

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 2021-06-24
    • 1970-01-01
    • 2019-11-11
    • 2014-04-20
    • 2020-04-11
    • 2010-09-22
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多