用c语言实现栈中的一些问题 解决后追加50分

来源:百度知道 编辑:UC知道 时间:2024/06/25 10:53:57
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR -1

typedef struct {
int *base;
int *top;
int stacksize;
}SqStack;
typedef int Status;

void InitStack(SqStack *S);
void Push(SqStack *S,int e);
Status Pop(SqStack *S,int *e);

main()
{
int *a;
SqStack OPTR,OPND;
InitStack(&OPTR);
InitStack(&OPND);
Push(&OPTR,1);
Pop(&OPTR,a);
printf("%d",a) ;
getch();
}

void InitStack(SqStack *S)
{
S->base = (int*)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S->base)
{
exit(OVERFLOW);
}

S->top = S->base;
S->stacksize =STACK_INIT_SIZE;
}

void Push(SqStack *S,int e){

这样就对了,你输出那里a定义的是指针类型,不能直接那样输出,应该定义个常量来输出以下是修改后的程序
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR -1

typedef struct {
int *base;
int *top;
int stacksize;
}SqStack;
typedef int Status;

void InitStack(SqStack *S);
void Push(SqStack *S,int e);
Status Pop(SqStack *S,int *e);

void main()
{ int m;
int *a=new Status;
SqStack OPTR;
InitStack(&OPTR);
Push(&OPTR,1);
m=Pop(&OPTR,a);
printf("%d",m) ;
getch();
}

void InitStack(SqStack *S)
{
S->base = (int*)malloc(STACK_INIT_SIZE * sizeof(int));
if(!S->base)
{
exit(OVERFLOW);
}

S->top = S->base;
S->stacksize =STACK_INIT_S