在C中实现堆栈的问题
我尝试用C实现堆栈。这是我的代码,我运行了代码,代码以: 线程1:EXC\u错误\u访问错误 我很困惑,不知道出了什么问题,有人能调试y代码吗?谢谢 我还有一个问题,为什么command+k键对我不起作用?我得一行一行地缩进在C中实现堆栈的问题,c,stack,C,Stack,我尝试用C实现堆栈。这是我的代码,我运行了代码,代码以: 线程1:EXC\u错误\u访问错误 我很困惑,不知道出了什么问题,有人能调试y代码吗?谢谢 我还有一个问题,为什么command+k键对我不起作用?我得一行一行地缩进 #include <stdio.h> #include <stdlib.h> #define init_size 10 #define increment 1 typedef struct sqStack { int* top;
#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;
}
#包括
#包括
#定义初始大小10
#定义增量1
typedef结构sqStack
{
int*top;
整数*基数;
int堆栈大小;
}sqStack;
int init_堆栈(sqStack*sq)
{
如果(sq->base==NULL)
{
sq->base=(int*)malloc(init_size*sizeof(int));//线程1:EXC_BAD_访问
}
如果(sq->base==NULL)退出(-1);
sq->堆栈大小=初始大小;
sq->top=sq->base;
返回1;
}
int push(sqStack*sq,int e)
{
如果(sq==NULL)退出(-1);
如果(sq->顶部sq->底部==sq->堆栈大小)
{
int*q=(int*)realloc(sq->base,
(初始大小+增量)*sizeof(int);
如果(q==NULL)退出(-1);
其他的
{
sq->base=q;
sq->堆栈大小+=增量;
sq->top=sq->base+sq->堆栈大小;
}
*sq->top++=e;
}
返回1;
}
int-pop(sqStack*sq,int*e)
{
如果(sq==NULL)退出(-1);
如果(sq->base==sq->top)退出(-1);
sq->top-=1;
*e=*sq->top;
返回1;
}
int空(sqStack*sq)
{
如果(sq->base==sq->top)返回1;
否则返回0;
}
int main()
{
sqStack*sq=NULL;
初始堆栈(sq);
推(sq,1);
int*e=(int*)malloc(sizeof(int));
pop(sq,e);
printf(“%d\n”,*e);
返回0;
}
int init_stack(sqStack*sq)
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
int push中(sqStack*sq,int e)
}
}
*sq->top++=e;
return 1;
}
sqStack sq;
init_stack(&sq);
push(&sq,1);
int e;
pop(sq,&e);
printf("%d\n",e);
int main()中
int*e=(int*)malloc(sizeof(int))代码>只需使用inte代码>
应该是
}
}
*sq->top++=e;
return 1;
}
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;
}
#包括
#包括
#定义初始大小10
#定义增量1
typedef结构sqStack{
int*top;
整数*基数;
int堆栈大小;
}sqStack;
int init_堆栈(sqStack*sq){
sq->base=(int*)malloc(init_size*sizeof(int));
如果(sq->base==NULL)退出(-1);
sq->堆栈大小=初始大小;
sq->top=sq->base;
返回1;
}
int push(sqStack*sq,int e){
如果(sq==NULL)退出(-1);
如果(sq->top-sq->base==sq->stack\u size){
int*q=(int*)realloc(sq->base,(初始大小+增量)*sizeof(int));
if(q==NULL)
出口(-1);
否则{
sq->base=q;
sq->堆栈大小+=增量;
sq->top=sq->base+sq->堆栈大小;
}
}
*sq->top++=e;
返回1;
}
int-pop(sqStack*sq,int*e){
如果(sq==NULL)退出(-1);
如果(sq->base==sq->top)退出(-1);
sq->top-=1;
*e=*sq->top;
返回1;
}
int空(sqStack*sq){
如果(sq->base==sq->top)
返回1;
其他的
返回0;
}
int main(){
sq堆栈sq;
初始堆栈(&sq);
推送(&sq,1);
INTE;
流行音乐(&sq,&e);
printf(“%d\n”,e);
返回0;
}
您正在传递一个NULL
指针,指向尝试取消引用它的init_stack
。那么我应该如何修复它?在使用指针之前,必须像sqStack*sq=&stack代码>非sqStack*sq=NULL代码>@liu sqStack*sq和sqStack&sq有什么区别??为什么后者有效?sqStack&sq=NULL不是也一样吗??sqStack&sq代码> C++中引用使用,<代码> SqStas*SQ;<代码>指针在C和C++中使用,<代码> SqStA*sq=null;<代码>sq点空,sqStack堆栈;sqStack*sq=&stack代码>sq点堆栈
#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;
}