英文:
Stack implementation shows blank array in codeblock but works fine in online compiler
问题
我有一个简单的堆栈实现。我想在推送之前显示堆栈。所以如果我的堆栈大小是3,根据我的实现,它应该打印0 0 0
。但在Codeblocks(版本20.03)中显示为空白,在一些在线编译器中可以正常工作。
#include <stdio.h>
#include <stdbool.h>
void init_stack(int);
void push(int);
int pop();
int peek();
void print();
int top = -1;
int stack_size;
int stack[];
int main()
{
printf("您的堆栈大小:");
scanf("%d", &stack_size);
printf("此堆栈仅允许整数值!");
stack[stack_size];
init_stack(stack_size);
bool t = true;
while(t)
{
printf("\n\n1. 推送\n2. 弹出\n3. 窥视\n4. isEmpty\n5. isFull\n6. 打印堆栈\n7. 退出");
printf("\n\n选择:");
int c;
scanf("%d", &c);
int Element;
switch(c)
{
case 1 :
printf("\n\n元素:");
scanf("%d", &Element);
if(top < stack_size-1)
{
push(Element);
printf("\n成功...");
}
else printf("\n\t堆栈溢出!");
break;
case 2 :
if(top > -1)
{
Element = pop();
printf("\n弹出的元素是:%d", Element);
}
else printf("\n\t堆栈下溢!");
break;
case 3 :
if(top > -1)
{
Element = peek();
printf("\n顶部元素是:%d", Element);
}
else printf("\n\t");
break;
case 4 :
if(top > -1) printf("\n堆栈不为空。");
else printf("\n堆栈为空!");
break;
case 5 :
if(top < stack_size-1) printf("\n堆栈未满。");
else printf("\n堆栈已满!");
break;
case 6 :
printf("\n\n当前堆栈:");
print();
break;
default :
t = false;
}
}
return 0;
}
void push(int Element)
{
top++;
stack[top] = Element;
}
int pop()
{
int Element = stack[top];
stack[top] = 0; //从堆栈中删除元素
top--;
return Element;
}
int peek()
{
return stack[top];
}
void print()
{
int i;
for(i=0; i<stack_size; i++)
{
printf("\t%d", stack[i]);
}
}
void init_stack(int n)
{
int i;
for(i=0; i<n; i++)
{
stack[i] = 0;
}
}
英文:
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
#include <stdio.h>
#include <stdbool.h>
void init_stack(int);
void push(int);
int pop();
int peek();
void print();
int top = -1;
int stack_size;
int stack[];
int main()
{
printf("Size of your stack : ");
scanf("%d", &stack_size);
printf("This Stack Allows Only Integer Value!");
stack[stack_size];
init_stack(stack_size);
bool t = true;
while(t)
{
printf("\n\n1. Push\n2. Pop\n3. Peek\n4. isEmpty\n5. isFull\n6. Print Stack\n7. Exit");
printf("\n\nChoose : ");
int c;
scanf("%d", &c);
int Element;
switch(c)
{
case 1 :
printf("\n\nElement : ");
scanf("%d", &Element);
if(top < stack_size-1)
{
push(Element);
printf("\nSuccessful...");
}
else printf("\n\tStack overflow!");
break;
case 2 :
if(top > -1)
{
Element = pop();
printf("\nPopped element is : %d", Element);
}
else printf("\n\tStack underflow!");
break;
case 3 :
if(top > -1)
{
Element = peek();
printf("\nTop element is : %d", Element);
}
else printf("\n\t");
break;
case 4 :
if(top > -1) printf("\nStack is not empty.");
else printf("\nStack is empty!");
break;
case 5 :
if(top < stack_size-1) printf("\nStack is not full.");
else printf("\nStack is full!");
break;
case 6 :
printf("\n\nCurrent stack :");
print();
break;
default :
t = false;
}
}
return 0;
}
void push(int Element)
{
top++;
stack[top] = Element;
}
int pop()
{
int Element = stack[top];
stack[top] = 0; //deleting the element from the stack
top--;
return Element;
}
int peek()
{
return stack[top];
}
void print()
{
int i;
for(i=0; i<stack_size; i++)
{
printf("\t%d", stack[i]);
}
}
void init_stack(int n)
{
int i;
for(i=0; i<n; i++)
{
stack[i] = 0;
}
}
Screenshots
Codeblocks
Please point out my mistakes.
答案1
得分: 1
避免全局变量。通过函数参数将您的“堆栈”传递给对其进行操作的函数。
为了实现这一点,您需要将所有堆栈信息作为一个值传递。您可以使用一个结构体来实现这一点。
使用动态内存分配来实现这一点。
例如:
typedef struct stack {
size_t capacity;
size_t top;
int *stack;
} stack_t;
stack_t *init_stack(size_t size) {
stack_t *new_stack = malloc(sizeof(stack_t));
if (!new_stack) return NULL;
new_stack->stack = calloc(size, sizeof(int));
if (!new_stack->stack) {
free(new_stack);
return NULL;
}
return new_stack;
}
bool push(stack_t *stack, int value) {
if (!stack || stack->top >= stack->capacity - 1) {
return false;
}
stack->stack[stack->top++] = value;
return true;
}
// 堆栈的其他操作
然后在 main
中,您可以写:
int main(void) {
size_t size;
if (scanf("%zu", &size) != 1) return 1;
stack_t *stack = init_stack(size);
if (!stack) return 1;
// 对您的堆栈进行操作。
// 释放您的堆栈
return 0;
}
英文:
-
Avoid global variables. Pass your "stack" to functions which work on it via a function argument.
-
To accomplish this you need all of your stack info as a value. You can use a struct to accomplish this.
-
Use dynamic memory allocation to accomplish this.
E.g.
typedef struct stack {
size_t capacity;
size_t top;
int *stack;
} stack_t;
stack_t *init_stack(size_t size) {
stack_t *new_stack = malloc(sizeof(stack_t));
if (!new_stack) return NULL;
new_stack->stack = calloc(size, sizeof(int));
if (!new_stack->stack) {
free(new_stack);
return NULL;
}
return new_stack;
}
bool push(stack_t *stack, int value) {
if (!stack || stack->top >= stack->capacity - 1) {
return false;
}
stack->stack[stack->top++] = value;
return true;
}
// the other operations on a stack
Then in main
you can now write:
int main(void) {
size_t size;
if (scanf("%zu", &size) != 1) return 1;
stack_t *stack = init_stack(size);
if (!stack) return 1;
// operations on your stack.
// free your stack
return 0;
}
</details>
# 答案2
**得分**: 0
感谢 @[WeatherVane][1] 提供的解决方案。
```c
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> // 添加了 malloc
void init_stack(int);
void push(int);
int pop();
int peek();
void print();
int top = -1;
int stack_size;
int* stack; // 删除了 []
int main()
{
printf("栈的大小: ");
scanf("%d", &stack_size);
stack = (int*)malloc(sizeof(int) * stack_size); // 修正了 malloc 的使用
printf("此栈只允许整数值!");
init_stack(stack_size);
bool t = true;
while (t)
{
printf("\n\n1. 入栈\n2. 出栈\n3. 查看栈顶元素\n4. 判断栈是否为空\n5. 判断栈是否已满\n6. 打印栈\n7. 退出");
printf("\n\n选择: ");
int c;
scanf("%d", &c);
int Element;
switch (c)
{
case 1:
printf("\n\n元素: ");
scanf("%d", &Element);
if (top < stack_size - 1)
{
push(Element);
printf("\n入栈成功...");
}
else
{
printf("\n\t栈溢出!");
}
break;
case 2:
if (top > -1)
{
Element = pop();
printf("\n弹出的元素为: %d", Element);
}
else
{
printf("\n\t栈下溢!");
}
break;
case 3:
if (top > -1)
{
Element = peek();
printf("\n栈顶元素为: %d", Element);
}
else
{
printf("\n\t栈为空!");
}
break;
case 4:
if (top > -1)
{
printf("\n栈不为空。");
}
else
{
printf("\n栈为空!");
}
break;
case 5:
if (top < stack_size - 1)
{
printf("\n栈不满。");
}
else
{
printf("\n栈已满!");
}
break;
case 6:
printf("\n\n当前栈:");
print();
break;
default:
t = false;
}
}
free(stack); // 释放分配的内存
return 0;
}
void push(int Element)
{
top++;
stack[top] = Element;
}
int pop()
{
int Element = stack[top];
stack[top] = 0; // 从栈中删除元素
top--;
return Element;
}
int peek()
{
return stack[top];
}
void print()
{
int i;
for (i = 0; i <= top; i++)
{
printf("\t%d", stack[i]);
}
}
void init_stack(int n)
{
int i;
for (i = 0; i < n; i++)
{
stack[i] = 0;
}
}
英文:
Thanks @WeatherVane for the solution.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> // Added for malloc
void init_stack(int);
void push(int);
int pop();
int peek();
void print();
int top = -1;
int stack_size;
int* stack; // Removed []
int main()
{
printf("Size of your stack: ");
scanf("%d", &stack_size);
stack = (int*)malloc(sizeof(int) * stack_size); // Corrected malloc usage
printf("This Stack Allows Only Integer Value!");
init_stack(stack_size);
bool t = true;
while (t)
{
printf("\n\n1. Push\n2. Pop\n3. Peek\n4. isEmpty\n5. isFull\n6. Print Stack\n7. Exit");
printf("\n\nChoose: ");
int c;
scanf("%d", &c);
int Element;
switch (c)
{
case 1:
printf("\n\nElement: ");
scanf("%d", &Element);
if (top < stack_size - 1)
{
push(Element);
printf("\nSuccessful...");
}
else
{
printf("\n\tStack overflow!");
}
break;
case 2:
if (top > -1)
{
Element = pop();
printf("\nPopped element is: %d", Element);
}
else
{
printf("\n\tStack underflow!");
}
break;
case 3:
if (top > -1)
{
Element = peek();
printf("\nTop element is: %d", Element);
}
else
{
printf("\n\tStack is empty!");
}
break;
case 4:
if (top > -1)
{
printf("\nStack is not empty.");
}
else
{
printf("\nStack is empty!");
}
break;
case 5:
if (top < stack_size - 1)
{
printf("\nStack is not full.");
}
else
{
printf("\nStack is full!");
}
break;
case 6:
printf("\n\nCurrent stack:");
print();
break;
default:
t = false;
}
}
free(stack); // Free the allocated memory
return 0;
}
void push(int Element)
{
top++;
stack[top] = Element;
}
int pop()
{
int Element = stack[top];
stack[top] = 0; // Deleting the element from the stack
top--;
return Element;
}
int peek()
{
return stack[top];
}
void print()
{
int i;
for (i = 0; i <= top; i++)
{
printf("\t%d", stack[i]);
}
}
void init_stack(int n)
{
int i;
for (i = 0; i < n; i++)
{
stack[i] = 0;
}
}
答案3
得分: -1
这个在文件作用域中声明的数组:
int stack[];
是一个数组的试探性定义,等同于以下定义:
int stack[1];
它的单个元素被初始化为零。
所以这个声明没有意义。
在main
函数内部的这个语句:
stack[stack_size];
没有效果。它试图访问先前在文件作用域中声明的数组的不存在的元素,索引为stack_size
,前提是stack_size
不等于0
。
而不是在文件作用域中声明数组,你可以在main
函数中声明一个可变长度数组,如下所示:
int main()
{
printf("你的栈的大小:");
scanf("%d", &stack_size);
printf("这个栈只允许整数值!");
int stack[stack_size];
//...
在这种情况下,你需要修改你的函数,为传递的数组添加一个额外的参数,例如:
#include <string.h>;
//...
void init_stack(int stack[], int n)
{
memset(stack, 0, n * sizeof(int));
}
请注意,print
函数应该只输出栈中的实际值。所以它应该定义成如下方式:
void print(void)
{
for(int i = 0; i < top + 1; i++)
{
printf("\t%d", stack[i]);
}
}
但无论如何,最好的做法是不要使用单独的变量,而是将数组、变量top
和变量size
声明为一个结构体的数据成员。例如:
struct Stack
{
int size;
int top;
int *data;
};
英文:
This declaration of an array in the file scope
int stack[];
is a tentative definition of an array that is equivalent to the definition
int stack[1];
and its single element is zero initialized.
So this declaration does not make sense.
This statement within main
:
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:
int main()
{
printf("Size of your stack : ");
scanf("%d", &stack_size);
printf("This Stack Allows Only Integer Value!");
int stack[stack_size];
//...
In this case you will need to change your functions adding one more parameter for the passed array to the functions as for example
#include <string.h>
//...
void init_stack( int stack[], int n )
{
memset( stack, 0, n * sizeof( int ) );
}
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
void print( void )
{
for( int i = 0; i < top + 1; i++ )
{
printf("\t%d", stack[i]);
}
}
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:
struct Stack
{
int size;
int top;
int *data;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论