栈的实现在代码块中显示为空数组,但在在线编译器中正常工作。

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

Stack implementation shows blank array in codeblock but works fine in online compiler

问题

我有一个简单的堆栈实现。我想在推送之前显示堆栈。所以如果我的堆栈大小是3,根据我的实现,它应该打印0 0 0。但在Codeblocks(版本20.03)中显示为空白,在一些在线编译器中可以正常工作。

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. void init_stack(int);
  4. void push(int);
  5. int pop();
  6. int peek();
  7. void print();
  8. int top = -1;
  9. int stack_size;
  10. int stack[];
  11. int main()
  12. {
  13. printf("您的堆栈大小:");
  14. scanf("%d", &stack_size);
  15. printf("此堆栈仅允许整数值!");
  16. stack[stack_size];
  17. init_stack(stack_size);
  18. bool t = true;
  19. while(t)
  20. {
  21. printf("\n\n1. 推送\n2. 弹出\n3. 窥视\n4. isEmpty\n5. isFull\n6. 打印堆栈\n7. 退出");
  22. printf("\n\n选择:");
  23. int c;
  24. scanf("%d", &c);
  25. int Element;
  26. switch(c)
  27. {
  28. case 1 :
  29. printf("\n\n元素:");
  30. scanf("%d", &Element);
  31. if(top < stack_size-1)
  32. {
  33. push(Element);
  34. printf("\n成功...");
  35. }
  36. else printf("\n\t堆栈溢出!");
  37. break;
  38. case 2 :
  39. if(top > -1)
  40. {
  41. Element = pop();
  42. printf("\n弹出的元素是:%d", Element);
  43. }
  44. else printf("\n\t堆栈下溢!");
  45. break;
  46. case 3 :
  47. if(top > -1)
  48. {
  49. Element = peek();
  50. printf("\n顶部元素是:%d", Element);
  51. }
  52. else printf("\n\t");
  53. break;
  54. case 4 :
  55. if(top > -1) printf("\n堆栈不为空。");
  56. else printf("\n堆栈为空!");
  57. break;
  58. case 5 :
  59. if(top < stack_size-1) printf("\n堆栈未满。");
  60. else printf("\n堆栈已满!");
  61. break;
  62. case 6 :
  63. printf("\n\n当前堆栈:");
  64. print();
  65. break;
  66. default :
  67. t = false;
  68. }
  69. }
  70. return 0;
  71. }
  72. void push(int Element)
  73. {
  74. top++;
  75. stack[top] = Element;
  76. }
  77. int pop()
  78. {
  79. int Element = stack[top];
  80. stack[top] = 0; //从堆栈中删除元素
  81. top--;
  82. return Element;
  83. }
  84. int peek()
  85. {
  86. return stack[top];
  87. }
  88. void print()
  89. {
  90. int i;
  91. for(i=0; i<stack_size; i++)
  92. {
  93. printf("\t%d", stack[i]);
  94. }
  95. }
  96. void init_stack(int n)
  97. {
  98. int i;
  99. for(i=0; i<n; i++)
  100. {
  101. stack[i] = 0;
  102. }
  103. }
英文:

I have a simple stack implementation. I want to show the stack before pushing
So If my stack size is 3, according to my implementation, it should print
0 0 0
But it is showing blank in codeblocks (version 20.03) but works fine in some online compiler

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdbool.h&gt;
  3. void init_stack(int);
  4. void push(int);
  5. int pop();
  6. int peek();
  7. void print();
  8. int top = -1;
  9. int stack_size;
  10. int stack[];
  11. int main()
  12. {
  13. printf(&quot;Size of your stack : &quot;);
  14. scanf(&quot;%d&quot;, &amp;stack_size);
  15. printf(&quot;This Stack Allows Only Integer Value!&quot;);
  16. stack[stack_size];
  17. init_stack(stack_size);
  18. bool t = true;
  19. while(t)
  20. {
  21. printf(&quot;\n\n1. Push\n2. Pop\n3. Peek\n4. isEmpty\n5. isFull\n6. Print Stack\n7. Exit&quot;);
  22. printf(&quot;\n\nChoose : &quot;);
  23. int c;
  24. scanf(&quot;%d&quot;, &amp;c);
  25. int Element;
  26. switch(c)
  27. {
  28. case 1 :
  29. printf(&quot;\n\nElement : &quot;);
  30. scanf(&quot;%d&quot;, &amp;Element);
  31. if(top &lt; stack_size-1)
  32. {
  33. push(Element);
  34. printf(&quot;\nSuccessful...&quot;);
  35. }
  36. else printf(&quot;\n\tStack overflow!&quot;);
  37. break;
  38. case 2 :
  39. if(top &gt; -1)
  40. {
  41. Element = pop();
  42. printf(&quot;\nPopped element is : %d&quot;, Element);
  43. }
  44. else printf(&quot;\n\tStack underflow!&quot;);
  45. break;
  46. case 3 :
  47. if(top &gt; -1)
  48. {
  49. Element = peek();
  50. printf(&quot;\nTop element is : %d&quot;, Element);
  51. }
  52. else printf(&quot;\n\t&quot;);
  53. break;
  54. case 4 :
  55. if(top &gt; -1) printf(&quot;\nStack is not empty.&quot;);
  56. else printf(&quot;\nStack is empty!&quot;);
  57. break;
  58. case 5 :
  59. if(top &lt; stack_size-1) printf(&quot;\nStack is not full.&quot;);
  60. else printf(&quot;\nStack is full!&quot;);
  61. break;
  62. case 6 :
  63. printf(&quot;\n\nCurrent stack :&quot;);
  64. print();
  65. break;
  66. default :
  67. t = false;
  68. }
  69. }
  70. return 0;
  71. }
  72. void push(int Element)
  73. {
  74. top++;
  75. stack[top] = Element;
  76. }
  77. int pop()
  78. {
  79. int Element = stack[top];
  80. stack[top] = 0; //deleting the element from the stack
  81. top--;
  82. return Element;
  83. }
  84. int peek()
  85. {
  86. return stack[top];
  87. }
  88. void print()
  89. {
  90. int i;
  91. for(i=0; i&lt;stack_size; i++)
  92. {
  93. printf(&quot;\t%d&quot;, stack[i]);
  94. }
  95. }
  96. void init_stack(int n)
  97. {
  98. int i;
  99. for(i=0; i&lt;n; i++)
  100. {
  101. stack[i] = 0;
  102. }
  103. }

Screenshots

Codeblocks

栈的实现在代码块中显示为空数组,但在在线编译器中正常工作。

Please point out my mistakes.

答案1

得分: 1

避免全局变量。通过函数参数将您的“堆栈”传递给对其进行操作的函数。

为了实现这一点,您需要将所有堆栈信息作为一个值传递。您可以使用一个结构体来实现这一点。

使用动态内存分配来实现这一点。

例如:

  1. typedef struct stack {
  2. size_t capacity;
  3. size_t top;
  4. int *stack;
  5. } stack_t;
  6. stack_t *init_stack(size_t size) {
  7. stack_t *new_stack = malloc(sizeof(stack_t));
  8. if (!new_stack) return NULL;
  9. new_stack->stack = calloc(size, sizeof(int));
  10. if (!new_stack->stack) {
  11. free(new_stack);
  12. return NULL;
  13. }
  14. return new_stack;
  15. }
  16. bool push(stack_t *stack, int value) {
  17. if (!stack || stack->top >= stack->capacity - 1) {
  18. return false;
  19. }
  20. stack->stack[stack->top++] = value;
  21. return true;
  22. }
  23. // 堆栈的其他操作

然后在 main 中,您可以写:

  1. int main(void) {
  2. size_t size;
  3. if (scanf("%zu", &size) != 1) return 1;
  4. stack_t *stack = init_stack(size);
  5. if (!stack) return 1;
  6. // 对您的堆栈进行操作。
  7. // 释放您的堆栈
  8. return 0;
  9. }
英文:
  1. Avoid global variables. Pass your "stack" to functions which work on it via a function argument.

  2. To accomplish this you need all of your stack info as a value. You can use a struct to accomplish this.

  3. Use dynamic memory allocation to accomplish this.

E.g.

  1. typedef struct stack {
  2. size_t capacity;
  3. size_t top;
  4. int *stack;
  5. } stack_t;
  6. stack_t *init_stack(size_t size) {
  7. stack_t *new_stack = malloc(sizeof(stack_t));
  8. if (!new_stack) return NULL;
  9. new_stack-&gt;stack = calloc(size, sizeof(int));
  10. if (!new_stack-&gt;stack) {
  11. free(new_stack);
  12. return NULL;
  13. }
  14. return new_stack;
  15. }
  16. bool push(stack_t *stack, int value) {
  17. if (!stack || stack-&gt;top &gt;= stack-&gt;capacity - 1) {
  18. return false;
  19. }
  20. stack-&gt;stack[stack-&gt;top++] = value;
  21. return true;
  22. }
  23. // the other operations on a stack

Then in main you can now write:

  1. int main(void) {
  2. size_t size;
  3. if (scanf(&quot;%zu&quot;, &amp;size) != 1) return 1;
  4. stack_t *stack = init_stack(size);
  5. if (!stack) return 1;
  6. // operations on your stack.
  7. // free your stack
  8. return 0;
  9. }
  10. </details>
  11. # 答案2
  12. **得分**: 0
  13. 感谢 @[WeatherVane][1] 提供的解决方案。
  14. ```c
  15. #include &lt;stdio.h&gt;
  16. #include &lt;stdbool.h&gt;
  17. #include &lt;stdlib.h&gt; // 添加了 malloc
  18. void init_stack(int);
  19. void push(int);
  20. int pop();
  21. int peek();
  22. void print();
  23. int top = -1;
  24. int stack_size;
  25. int* stack; // 删除了 []
  26. int main()
  27. {
  28. printf("栈的大小: ");
  29. scanf("%d", &stack_size);
  30. stack = (int*)malloc(sizeof(int) * stack_size); // 修正了 malloc 的使用
  31. printf("此栈只允许整数值!");
  32. init_stack(stack_size);
  33. bool t = true;
  34. while (t)
  35. {
  36. printf("\n\n1. 入栈\n2. 出栈\n3. 查看栈顶元素\n4. 判断栈是否为空\n5. 判断栈是否已满\n6. 打印栈\n7. 退出");
  37. printf("\n\n选择: ");
  38. int c;
  39. scanf("%d", &c);
  40. int Element;
  41. switch (c)
  42. {
  43. case 1:
  44. printf("\n\n元素: ");
  45. scanf("%d", &Element);
  46. if (top < stack_size - 1)
  47. {
  48. push(Element);
  49. printf("\n入栈成功...");
  50. }
  51. else
  52. {
  53. printf("\n\t栈溢出!");
  54. }
  55. break;
  56. case 2:
  57. if (top > -1)
  58. {
  59. Element = pop();
  60. printf("\n弹出的元素为: %d", Element);
  61. }
  62. else
  63. {
  64. printf("\n\t栈下溢!");
  65. }
  66. break;
  67. case 3:
  68. if (top > -1)
  69. {
  70. Element = peek();
  71. printf("\n栈顶元素为: %d", Element);
  72. }
  73. else
  74. {
  75. printf("\n\t栈为空!");
  76. }
  77. break;
  78. case 4:
  79. if (top > -1)
  80. {
  81. printf("\n栈不为空。");
  82. }
  83. else
  84. {
  85. printf("\n栈为空!");
  86. }
  87. break;
  88. case 5:
  89. if (top < stack_size - 1)
  90. {
  91. printf("\n栈不满。");
  92. }
  93. else
  94. {
  95. printf("\n栈已满!");
  96. }
  97. break;
  98. case 6:
  99. printf("\n\n当前栈:");
  100. print();
  101. break;
  102. default:
  103. t = false;
  104. }
  105. }
  106. free(stack); // 释放分配的内存
  107. return 0;
  108. }
  109. void push(int Element)
  110. {
  111. top++;
  112. stack[top] = Element;
  113. }
  114. int pop()
  115. {
  116. int Element = stack[top];
  117. stack[top] = 0; // 从栈中删除元素
  118. top--;
  119. return Element;
  120. }
  121. int peek()
  122. {
  123. return stack[top];
  124. }
  125. void print()
  126. {
  127. int i;
  128. for (i = 0; i <= top; i++)
  129. {
  130. printf("\t%d", stack[i]);
  131. }
  132. }
  133. void init_stack(int n)
  134. {
  135. int i;
  136. for (i = 0; i < n; i++)
  137. {
  138. stack[i] = 0;
  139. }
  140. }
英文:

Thanks @WeatherVane for the solution.

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdbool.h&gt;
  3. #include &lt;stdlib.h&gt; // Added for malloc
  4. void init_stack(int);
  5. void push(int);
  6. int pop();
  7. int peek();
  8. void print();
  9. int top = -1;
  10. int stack_size;
  11. int* stack; // Removed []
  12. int main()
  13. {
  14. printf(&quot;Size of your stack: &quot;);
  15. scanf(&quot;%d&quot;, &amp;stack_size);
  16. stack = (int*)malloc(sizeof(int) * stack_size); // Corrected malloc usage
  17. printf(&quot;This Stack Allows Only Integer Value!&quot;);
  18. init_stack(stack_size);
  19. bool t = true;
  20. while (t)
  21. {
  22. printf(&quot;\n\n1. Push\n2. Pop\n3. Peek\n4. isEmpty\n5. isFull\n6. Print Stack\n7. Exit&quot;);
  23. printf(&quot;\n\nChoose: &quot;);
  24. int c;
  25. scanf(&quot;%d&quot;, &amp;c);
  26. int Element;
  27. switch (c)
  28. {
  29. case 1:
  30. printf(&quot;\n\nElement: &quot;);
  31. scanf(&quot;%d&quot;, &amp;Element);
  32. if (top &lt; stack_size - 1)
  33. {
  34. push(Element);
  35. printf(&quot;\nSuccessful...&quot;);
  36. }
  37. else
  38. {
  39. printf(&quot;\n\tStack overflow!&quot;);
  40. }
  41. break;
  42. case 2:
  43. if (top &gt; -1)
  44. {
  45. Element = pop();
  46. printf(&quot;\nPopped element is: %d&quot;, Element);
  47. }
  48. else
  49. {
  50. printf(&quot;\n\tStack underflow!&quot;);
  51. }
  52. break;
  53. case 3:
  54. if (top &gt; -1)
  55. {
  56. Element = peek();
  57. printf(&quot;\nTop element is: %d&quot;, Element);
  58. }
  59. else
  60. {
  61. printf(&quot;\n\tStack is empty!&quot;);
  62. }
  63. break;
  64. case 4:
  65. if (top &gt; -1)
  66. {
  67. printf(&quot;\nStack is not empty.&quot;);
  68. }
  69. else
  70. {
  71. printf(&quot;\nStack is empty!&quot;);
  72. }
  73. break;
  74. case 5:
  75. if (top &lt; stack_size - 1)
  76. {
  77. printf(&quot;\nStack is not full.&quot;);
  78. }
  79. else
  80. {
  81. printf(&quot;\nStack is full!&quot;);
  82. }
  83. break;
  84. case 6:
  85. printf(&quot;\n\nCurrent stack:&quot;);
  86. print();
  87. break;
  88. default:
  89. t = false;
  90. }
  91. }
  92. free(stack); // Free the allocated memory
  93. return 0;
  94. }
  95. void push(int Element)
  96. {
  97. top++;
  98. stack[top] = Element;
  99. }
  100. int pop()
  101. {
  102. int Element = stack[top];
  103. stack[top] = 0; // Deleting the element from the stack
  104. top--;
  105. return Element;
  106. }
  107. int peek()
  108. {
  109. return stack[top];
  110. }
  111. void print()
  112. {
  113. int i;
  114. for (i = 0; i &lt;= top; i++)
  115. {
  116. printf(&quot;\t%d&quot;, stack[i]);
  117. }
  118. }
  119. void init_stack(int n)
  120. {
  121. int i;
  122. for (i = 0; i &lt; n; i++)
  123. {
  124. stack[i] = 0;
  125. }
  126. }

答案3

得分: -1

这个在文件作用域中声明的数组:

  1. int stack[];

是一个数组的试探性定义,等同于以下定义:

  1. int stack[1];

它的单个元素被初始化为零。

所以这个声明没有意义。

main函数内部的这个语句:

  1. stack[stack_size];

没有效果。它试图访问先前在文件作用域中声明的数组的不存在的元素,索引为stack_size,前提是stack_size不等于0

而不是在文件作用域中声明数组,你可以在main函数中声明一个可变长度数组,如下所示:

  1. int main()
  2. {
  3. printf("你的栈的大小:");
  4. scanf("%d", &stack_size);
  5. printf("这个栈只允许整数值!");
  6. int stack[stack_size];
  7. //...

在这种情况下,你需要修改你的函数,为传递的数组添加一个额外的参数,例如:

  1. #include <string.h>;
  2. //...
  3. void init_stack(int stack[], int n)
  4. {
  5. memset(stack, 0, n * sizeof(int));
  6. }

请注意,print函数应该只输出栈中的实际值。所以它应该定义成如下方式:

  1. void print(void)
  2. {
  3. for(int i = 0; i < top + 1; i++)
  4. {
  5. printf("\t%d", stack[i]);
  6. }
  7. }

但无论如何,最好的做法是不要使用单独的变量,而是将数组、变量top和变量size声明为一个结构体的数据成员。例如:

  1. struct Stack
  2. {
  3. int size;
  4. int top;
  5. int *data;
  6. };
英文:

This declaration of an array in the file scope

  1. int stack[];

is a tentative definition of an array that is equivalent to the definition

  1. int stack[1];

and its single element is zero initialized.

So this declaration does not make sense.

This statement within main:

  1. stack[stack_size];

has no effect. It is an attempt to access a non-existent element of the previously declared file scope array with the index stack_size provided that stack_size is not equal to 0.

Instead of the file scope array you could declare a variable length array within the function main like:

  1. int main()
  2. {
  3. printf(&quot;Size of your stack : &quot;);
  4. scanf(&quot;%d&quot;, &amp;stack_size);
  5. printf(&quot;This Stack Allows Only Integer Value!&quot;);
  6. int stack[stack_size];
  7. //...

In this case you will need to change your functions adding one more parameter for the passed array to the functions as for example

  1. #include &lt;string.h&gt;
  2. //...
  3. void init_stack( int stack[], int n )
  4. {
  5. memset( stack, 0, n * sizeof( int ) );
  6. }

Pay attention to that the function print should output only actual values in the stack. So it should be defined something like the following way

  1. void print( void )
  2. {
  3. for( int i = 0; i &lt; top + 1; i++ )
  4. {
  5. printf(&quot;\t%d&quot;, stack[i]);
  6. }
  7. }

But in any case it would be much better instead of using separate variables as the array, the variable top and the variable size to declare a structure that will contain these entities as its data members. For example:

  1. struct Stack
  2. {
  3. int size;
  4. int top;
  5. int *data;
  6. };

huangapple
  • 本文由 发表于 2023年7月7日 02:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631787.html
匿名

发表评论

匿名网友

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

确定