代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 10;
#define STACKINCREMENT 5;
typedef struct{
int *base;
int *top;
int stacksize;
}sqstack,*sqstackp;
int initstack(sqstackp &s){
s->base=(int *)malloc(sizeof(int));
if(!s->base)
return 0;
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return 1;
}
int push(sqstackp s,int e){
if(s->top-s->base>=s->stacksize){
s->base=(int *)realloc(s->base,10*sizeof(int));
if(!s->base)
return 0;
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return 1;
}
int pop(sqstackp s,int &e){
if(s->top==s->base)
return 0;
e=*–s->top;
return 1;
}
int stackempty(sqstackp s){
if(s->top==s->base)
return 1;
else
return 0;
}
sqstackp S;
int N,e;
int main(){
initstack(S);
scanf(“%d”,N);
while(N){
push(S,N%8);
N=N/8;
}
while(!stackempty(S)){
pop(S,e);
printf(“%d”,e);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 10;
#define STACKINCREMENT 5;
typedef struct{
int *base;
int *top;
int stacksize;
}sqstack,*sqstackp;
int initstack(sqstackp &s){
s->base=(int *)malloc(sizeof(int));
if(!s->base)
return 0;
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return 1;
}
int push(sqstackp s,int e){
if(s->top-s->base>=s->stacksize){
s->base=(int *)realloc(s->base,10*sizeof(int));
if(!s->base)
return 0;
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return 1;
}
int pop(sqstackp s,int &e){
if(s->top==s->base)
return 0;
e=*–s->top;
return 1;
}
int stackempty(sqstackp s){
if(s->top==s->base)
return 1;
else
return 0;
}
sqstackp S;
int N,e;
int main(){
initstack(S);
scanf(“%d”,N);
while(N){
push(S,N%8);
N=N/8;
}
while(!stackempty(S)){
pop(S,e);
printf(“%d”,e);
}
return 0;
}
解决方案
100
你这个很多处小错误:
1.需要为S分配空间才行,否则它指向的是未知区域(野指针),S = (sqstackp)malloc(sizeof(sqstack));
2.scanf(“%d”, N);少了一个&
3.s->base = (int *)malloc(sizeof(int));应该是s->base = (int *)malloc(sizeof(int)* STACK_INIT_SIZE);
4.#define STACK_INIT_SIZE 10后面多了分号
1.需要为S分配空间才行,否则它指向的是未知区域(野指针),S = (sqstackp)malloc(sizeof(sqstack));
2.scanf(“%d”, N);少了一个&
3.s->base = (int *)malloc(sizeof(int));应该是s->base = (int *)malloc(sizeof(int)* STACK_INIT_SIZE);
4.#define STACK_INIT_SIZE 10后面多了分号
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #define STACK_INIT_SIZE 10 #define STACKINCREMENT 5 typedef struct{ int *base; int *top; int stacksize; }sqstack, *sqstackp; int initstack(sqstackp &s){ s->base = (int *)malloc(sizeof(int)* STACK_INIT_SIZE); if (!s->base) return 0; s->top = s->base; s->stacksize = STACK_INIT_SIZE; return 1; } int push(sqstackp s, int e){ if (s->top - s->base >= s->stacksize){ s->base = (int *)realloc(s->base, 10 * sizeof(int)); if (!s->base) return 0; s->top = s->base + s->stacksize; s->stacksize += STACKINCREMENT; } *s->top++ = e; return 1; } int pop(sqstackp s, int &e){ if (s->top == s->base) return 0; e = *--s->top; return 1; } int stackempty(sqstackp s){ if (s->top == s->base) return 1; else return 0; } sqstackp S; int N, e; int main(){ S = (sqstackp)malloc(sizeof(sqstack)); initstack(S); scanf("%d", &N); while (N){ push(S, N % 8); N = N / 8; } while (!stackempty(S)){ pop(S, e); printf("%d", e); } return 0; }