69,336
社区成员
发帖
与我相关
我的任务
分享
//程序无法运行 如何修改
//push函数中的最后一句*S.top++=e;有问题
//access violation writing location
//#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
//栈的顺序存储表示
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配量
#define SElemType int
typedef struct{
SElemType *base; //在栈构之前和销毁之后,base的值为NULL
SElemType *top; //栈顶指针
int stacksize; //当前已分配的存储空间,以元素为单位
}SqStack;
void InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) {
cout<<"error"<<endl;
exit(0);
}
//S.base = S.top;
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
}
void Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) {
cout<<"error"<<endl;
return;
}
e=*--S.top;
}
void Push(SqStack &S, SElemType e)
{
printf("%s, %p, %p\n", __func__, S.base, S.top);
int type_len = sizeof(SElemType);
if((S.top - S.base) / type_len >=S.stacksize) { //栈满,追加存储空间
S.base = (SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) {
cout<<"error"<<endl;
exit(0);
}
//S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++=e;
}
int StackEmpty(SqStack &S)
{
if(S.top==S.base)
return 1;
else
return 0;
}
int main()
{
int N;
SElemType e;
SqStack S1;
printf("%s, %p, %p\n", __func__, S1.base, S1.top);
InitStack(S1);
printf("%s, %p, %p\n", __func__, S1.base, S1.top);
while(scanf("%d", &N) != EOF)
{
while (N) {
Push(S1, N%8); //进栈
N=N/8;
}
while(!StackEmpty(S1)) {
Pop(S1,e);
printf("%d",e);
}
}
return 0;
}
//S.base = S.top;
S.top = S.base;