动态数组堆栈

huangapple go评论97阅读模式
英文:

Dynamic array based stack

问题

我正在尝试创建一个基于动态数组的栈,当我尝试将元素推入已满的数组时,出现了索引越界错误。我还使数组具有通用性,以容纳所有类型的栈。

  1. import java.util.Arrays;
  2. import java.util.Iterator;
  3. public class Stack<T> {
  4. private int topStack;
  5. private int stackSize;
  6. private T[] stack;
  7. // 构造函数
  8. public Stack(T[] arr) {
  9. stack = arr;
  10. stackSize = arr.length;
  11. topStack = -1;
  12. }
  13. // isEmpty方法
  14. public boolean isEmpty() {
  15. if (topStack == -1)
  16. return true;
  17. return false;
  18. }
  19. // isFull方法
  20. public boolean isFull() {
  21. if ((topStack + 1) == stackSize)
  22. return true;
  23. return false;
  24. }
  25. // 增加数组大小的方法 &lt;-----------------------------------
  26. public void incrementArray() {
  27. T[] temp = (T[]) new Object[stackSize * 2];
  28. System.arraycopy(stack, 0, temp, 0, stack.length);
  29. stack = temp;
  30. stackSize = stackSize * 2;
  31. }
  32. // 减小数组大小的方法
  33. public void decrementArray() {
  34. stackSize = stackSize / 2;
  35. T[] temp = (T[]) new Object[stackSize];
  36. System.arraycopy(stack, 0, temp, 0, stackSize);
  37. stack = temp;
  38. }
  39. // 推入元素到栈顶的push方法
  40. public void push(T element) {
  41. if (isFull())
  42. incrementArray();
  43. topStack = topStack + 1;
  44. stack[topStack] = element;
  45. }
  46. // 查看栈顶元素而不弹出的peek方法
  47. public T peek() {
  48. return stack[topStack];
  49. }
  50. // 弹出顶部元素的pop方法,复制顶部并返回副本
  51. public T pop() throws EmptyStackException {
  52. if (isEmpty()) {
  53. throw new EmptyStackException();
  54. }
  55. int temp = topStack + 1;
  56. if (temp &lt; stackSize / 2)
  57. decrementArray();
  58. return stack[topStack--];
  59. }
  60. public static void main(String[] args) {
  61. Stack operands = new Stack<>(new Integer[0]);
  62. operands.push(2);
  63. operands.push(1);
  64. }
  65. }

我正在尝试增加栈的大小,而不是使其超出边界。

英文:

I'm trying to create a dynamic array based stack and I am getting an index out of bound error when I try to push elements elements onto a full array. I also made the array generic to accommodate all types of stacks.

  1. import java.util.Arrays;
  2. import java.util.Iterator;
  3. public class Stack&lt;T&gt; {
  4. private int topStack;
  5. private int stackSize;
  6. private T[] stack;
  7. // Constructor
  8. public Stack(T[] arr) {
  9. stack = arr;
  10. stackSize = arr.length;
  11. topStack = -1;
  12. }
  13. // isEmpty
  14. public boolean isEmpty() {
  15. if (topStack == -1)
  16. return true;
  17. return false;
  18. }
  19. // isFull method
  20. public boolean isFull() {
  21. if ((topStack + 1) == stackSize)
  22. return true;
  23. return false;
  24. }
  25. // increment array by 10 spaces &lt;-----------------------------------
  26. public void incrementArray() {
  27. T[] temp = (T[]) new Object[stackSize*2];
  28. System.arraycopy(stack, 0, temp, 0, stack.length);
  29. stack=temp;
  30. stackSize=stackSize*2;
  31. }
  32. // decrement array
  33. public void decrementArray() {
  34. stackSize=stackSize/2;
  35. T[] temp = (T[]) new Object[stackSize];
  36. System.arraycopy(stack, 0, temp, 0, stackSize);
  37. stack=temp;
  38. }
  39. // push method which adds element to top of stack
  40. public void push(T element) {
  41. if (isFull())
  42. incrementArray();
  43. topStack=topStack+1;
  44. stack[topStack] = element;
  45. }
  46. // peek method which shows top of stack without popping it
  47. public T peek() {
  48. return stack[topStack];
  49. }
  50. // pop which copies top of stack, deletes top and returns copy
  51. public T pop() throws EmptyStackException {
  52. if (isEmpty()) {
  53. throw new EmptyStackException();
  54. }
  55. int temp = topStack+1;
  56. if(temp&lt;stackSize/2)
  57. decrementArray();
  58. return stack[topStack--];
  59. }
  60. public static void main(String[] args) {
  61. Stack operands = new Stack&lt;&gt;(new Integer[0]);
  62. operands.push(2);
  63. operands.push(1);
  64. }
  65. }

I'm trying to increase the stack size instead of having it overflow out of bounds.

答案1

得分: 2

你应该为你的堆栈设置一个初始的 size &gt; 0。目前,你的初始 stackSize 为0。猜猜看 stackSize*2 等于多少?另一个观察是,你创建了一个泛型堆栈,但在 main 中创建它时没有指定类型。

另外,请注意你可以将以下代码段:

  1. public boolean isEmpty() {
  2. if (topStack == -1)
  3. return true;
  4. return false;
  5. }

改为:

  1. public boolean isEmpty() {
  2. return topStack == -1;
  3. }

你可以在其他返回 boolean 的方法中进行类似的更改。

当你的代码不按预期运行时,思考一下发生了什么以及可能的原因。在你的代码中广泛使用打印语句是检查关键值是否符合预期的良好第一步。更复杂的方法是使用调试工具逐步执行你的程序,查看各个字段的值。

英文:

You should give your stack an initial size &gt; 0. As it stands, your initial stackSize is 0. And guess what stackSize*2 is equal to? And another observation is that you created a generic stack but did not specify a type when creating it in main

Also, note that you can change

  1. public boolean isEmpty() {
  2. if (topStack == -1)
  3. return true;
  4. return false;
  5. }

to

  1. public boolean isEmpty() {
  2. return topStack == -1;
  3. }

You can make similar changes in other methods that return a boolean.

When your code does not behave the way it should, think about what is going on and why that might happen. Placing ubiquitous print statements thru out your code is a good first step to check on key values to see if they are what you expect them to be. A more sophisticated method is to use a debugging tool to step thru your program as it executes to look at the values of various fields.

答案2

得分: 0

你应该使用动态分配内存的链表栈。这些栈永远不会溢出。只有当你的计算机内存已满时才会溢出。

  1. public class DynamicStack {
  2. static class Node {
  3. int data;
  4. Node next;
  5. Node(int d) {
  6. data = d;
  7. next = null;
  8. }
  9. }
  10. static class Stack {
  11. public static Node topHead;
  12. public static boolean isEmpty() {
  13. return topHead == null;
  14. }
  15. public static void push(int data) {
  16. Node newnode = new Node(data);
  17. if (isEmpty()) {
  18. topHead = newnode;
  19. return;
  20. }
  21. newnode.next = topHead;
  22. topHead = newnode;
  23. }
  24. public static int pop() {
  25. if (isEmpty()) {
  26. return -1;
  27. }
  28. int data = topHead.data;
  29. topHead = topHead.next;
  30. return data;
  31. }
  32. public static int peek() {
  33. if (isEmpty()) {
  34. return -1;
  35. }
  36. return topHead.data;
  37. }
  38. }
  39. public static void main(String[] args) {
  40. Stack st = new Stack();
  41. st.push(1);
  42. st.push(2);
  43. st.push(3);
  44. st.push(4);
  45. while (!st.isEmpty()) {
  46. System.out.println(st.peek());
  47. st.pop();
  48. }
  49. }
  50. }

希望这对你有用。

英文:

you should use linked list stack which is dynamically allocated memory. those stack never overflow. it is overflow when your computer memory will full

  1. public class DynamicStack {
  2. static class Node{
  3. int data;
  4. Node next;
  5. Node(int d){
  6. data = d;
  7. next = null;
  8. }
  9. }
  10. static class Stack{
  11. public static Node topHead;
  12. public static boolean isEmpty(){
  13. return topHead == null;
  14. }
  15. public static void push(int data){
  16. Node newnode = new Node(data);
  17. if(isEmpty()){
  18. topHead = newnode;
  19. return;
  20. }
  21. newnode.next = topHead;
  22. topHead = newnode;
  23. }
  24. public static int pop(){
  25. if (isEmpty()) {
  26. return -1;
  27. }
  28. int data = topHead.data;
  29. topHead = topHead.next;
  30. return data;
  31. }
  32. public static int peek(){
  33. if (isEmpty()) {
  34. return -1;
  35. }
  36. return topHead.data;
  37. }
  38. }
  39. public static void main(String[] args) {
  40. Stack st = new Stack();
  41. st.push(1);
  42. st.push(2);
  43. st.push(3);
  44. st.push(4);
  45. while (!st.isEmpty()) {
  46. System.out.println(st.peek());
  47. st.pop();
  48. }
  49. }
  50. }

i hope this is useful for you

huangapple
  • 本文由 发表于 2023年2月16日 05:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定