c - 在 C 中实现堆栈的问题
问题描述
我试图在 C 中实现堆栈。这是我的代码,我运行了代码,代码以:
线程 1:EXC_BAD_ACCESS 错误
我很困惑,不知道出了什么问题,任何人都可以调试你的代码吗?谢谢!
我还有一个问题,为什么command+k键对我不起作用?我必须逐行缩进。
#include <stdio.h>
#include <stdlib.h>
#define init_size 10
#define increment 1
typedef struct sqStack
{
int* top;
int* base;
int stack_size;
} sqStack;
int init_stack(sqStack* sq)
{
if(sq->base==NULL)
{
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
}
if(sq->base==NULL) exit(-1);
sq->stack_size=init_size;
sq->top=sq->base;
return 1;
}
int push(sqStack* sq, int e)
{
if(sq==NULL) exit(-1);
if(sq->top-sq->base==sq->stack_size)
{
int* q = (int*)realloc(sq->base,
(init_size+increment)*sizeof(int));
if(q==NULL) exit(-1);
else
{
sq->base=q;
sq->stack_size += increment;
sq->top=sq->base+sq->stack_size;
}
*sq->top++=e;
}
return 1;
}
int pop(sqStack* sq,int*e)
{
if(sq==NULL) exit(-1);
if(sq->base==sq->top) exit(-1);
sq->top-=1;
*e=*sq->top;
return 1;
}
int empty(sqStack* sq)
{
if(sq->base==sq->top) return 1;
else return 0;
}
int main()
{
sqStack* sq=NULL;
init_stack(sq);
push(sq,1);
int *e=(int*)malloc(sizeof(int));
pop(sq,e);
printf("%d\n",*e);
return 0;
}
解决方案
- 在功能
int init_stack(sqStack* sq)
上
if(sq->base==NULL)
{
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
}
应该:
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
- 在功能
int push(sqStack* sq, int e)
这
}
*sq->top++=e;
}
return 1;
}
应该
}
}
*sq->top++=e;
return 1;
}
- 在功能
int main()
不需要使用int *e=(int*)malloc(sizeof(int));
,只需使用int e;
这
sqStack* sq=NULL;
init_stack(sq);
push(sq,1);
int *e=(int*)malloc(sizeof(int));
pop(sq,e);
printf("%d\n",*e);
应该
sqStack sq;
init_stack(&sq);
push(&sq,1);
int e;
pop(sq,&e);
printf("%d\n",e);
以下code
可以工作:
#include <stdio.h>
#include <stdlib.h>
#define init_size 10
#define increment 1
typedef struct sqStack {
int* top;
int* base;
int stack_size;
} sqStack;
int init_stack(sqStack* sq) {
sq->base = (int*)malloc(init_size * sizeof(int));
if (sq->base == NULL) exit(-1);
sq->stack_size = init_size;
sq->top = sq->base;
return 1;
}
int push(sqStack* sq, int e) {
if (sq == NULL) exit(-1);
if (sq->top - sq->base == sq->stack_size) {
int* q = (int*)realloc(sq->base, (init_size + increment) * sizeof(int));
if (q == NULL)
exit(-1);
else {
sq->base = q;
sq->stack_size += increment;
sq->top = sq->base + sq->stack_size;
}
}
*sq->top++ = e;
return 1;
}
int pop(sqStack* sq, int* e) {
if (sq == NULL) exit(-1);
if (sq->base == sq->top) exit(-1);
sq->top -= 1;
*e = *sq->top;
return 1;
}
int empty(sqStack* sq) {
if (sq->base == sq->top)
return 1;
else
return 0;
}
int main() {
sqStack sq;
init_stack(&sq);
push(&sq, 1);
int e;
pop(&sq, &e);
printf("%d\n", e);
return 0;
}
推荐阅读
- excel - 如何在文件路径字符串中使用分配给变量的单元格值?
- reactjs - 将 redux devtools 与自己的商店连接
- c# - 无法使用内容同步操作 (syncrepl) 从 OpenLDAP 服务器获取已删除的项目
- build - 如何对诸如 Build/Rebuild 之类的 msbuild 内置目标设置条件?
- javascript - JavaScript:以像素为单位获取背景位置
- c# - 如何禁用 MaterialDesignInXaml 按钮动画?
- r - 我可以提取包含R中任何字符串向量的字符串吗
- mysql - Mysql:更新触发器错误:操作数应包含 1 列
- d3.js - d3 设置不同于 ZoomIdentity 的默认缩放配置
- sql - 当某些值重复而其他值不重复时,如何选择具有最新日期的值?