一道数据结构题:#include #include #define STACK_INIT_SIZE 100#define
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/11 00:41:17
一道数据结构题:
#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base) return ERROR;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base) return ERROR;
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top)=e;
S->top++;
return OK;
}
int Pop(SqStack *S,int *e)
{
if(S->top==S->base) return ERROR;
*e=*(S->top-1);
return OK;
}
int GetTop(SqStack S,int *e)
{
if(S.top==S.base) return ERROR;
S.top--;
*e=*(S.top);
return OK;
}
void main()
{
SqStack S;
int i,j,*p;
i=2,j=4;
InitStack(&S);
Push(&S,i);
Push(&S,j);
GetTop(S,p);
printf("%d",*p);
}
为什么运行有错
用VC++是这样的:
#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base) return ERROR;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{S->base=(int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base) return ERROR;
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top)=e;
S->top++;
return OK;
}
int Pop(SqStack *S,int *e)
{
if(S->top==S->base) return ERROR;
*e=*(S->top-1);
return OK;
}
int GetTop(SqStack S,int *e)
{
if(S.top==S.base) return ERROR;
S.top--;
*e=*(S.top);
return OK;
}
void main()
{
SqStack S;
int i,j,*p;
i=2,j=4;
InitStack(&S);
Push(&S,i);
Push(&S,j);
GetTop(S,p);
printf("%d",*p);
}
为什么运行有错
用VC++是这样的:
我试了一下,没有问题的,程序运行结果是4,就是栈顶元素的值
手上没有VC++,我是用GCC编译的
手上没有VC++,我是用GCC编译的
一道数据结构题:#include #include #define STACK_INIT_SIZE 100#define
#include #include #include #include #include /* define
英语翻译#include #include #include #include #define WIN_CONSOLE
#include#include#include#includeusing namespace std;#define
#include #include #define TRUE 1 #define FALSE 0 #define OK
ds18b20测温#include#include#define uchar unsigned char#define
英语翻译#include#include#define uchar unsigned char#define uint
#include#include#include#define LEN sizeof(struct LinkList)s
括号匹配检验程序#include#include#define TRUE 1#define FALSE 0#define
#include #include #define MAXLINE 1000int getline(char *line
#include#include#define MAXNUM 20struct stacktype{int stack[
判断矩阵是否可逆#include #include#include#include#define NUMBER 20 #