- В функции
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;
}