可以了,我帮你改了一下,现在可以运行了。
#include<stdio.h>#include<malloc.h>
#define STACK_INT_SIZE 10
typedef int ElemType
typedef struct
{ ElemType *base
ElemType *top
int stacksize /*当前已分配的存储空间*/
} SqStack
int InitStack(SqStack *S)//初始化栈
{ S->base=(ElemType *)malloc(STACK_INT_SIZE*sizeof(ElemType))
if(!S->base) return 0
S->top=S->base
S->stacksize=STACK_INT_SIZE
return 1
}
int Push(SqStack *S,ElemType e)//进栈
{ if(S->top-S->base==STACK_INT_SIZE)//栈满
return 0
else
{ *(S->top)=e
S->top++
}
}
int Pop(SqStack *S,ElemType *e)//出栈
{ if(S->top==S->base)//栈空
return 0
else
{ S->top--
*e=*(S->top)
}
}
int CreateStack(SqStack *S)
{ int e
if(S) //这里直接改为s
printf("Init Success!\n")
else
{ printf("Init Fail!\n")
return 0
}
printf("input data:(Terminated by inputing a character)\n")
while(scanf("%d",&e)&&e!=0)//加个条件,不然会变为死循环
Push(S,e)
return 1
}
void PrintStack(SqStack *S)//输出栈
{ ElemType e
while(Pop(S,&e))
printf("%3d",e)
}
int main()
{ SqStack ss
printf("1-createStack\n")
InitStack(&ss)//要先初始化栈再调用
CreateStack(&ss)
printf("2-Pop&Print\n")
PrintStack(&ss)
return 0
}
#define STACK_SIZE 100#define PUSH_POP_SUCCESS 1
#define PUSH_POP_ERROR 0
struct _stackbuf {
int _collection[STACK_SIZE]
int _top
}
typedef struct _stackbuf S_STACK
typedef unsigned int u_int_f
// 入栈
u_int_f push(S_STACK *stack, int d){
if (stack->_top >= STACK_SIZE) return PUSH_POP_ERROR
stack->_collection[stack->_top++] = d
return PUSH_POP_SUCCESS
}
// 出栈
u_int_f pop(S_STACK *stack, int *e){
if (!stack->_top) return PUSH_POP_ERROR
*e=stack->_collection[--(stack->_top)]
return PUSH_POP_SUCCESS
}
int main(){
S_STACK stack = { {0},0 }
push(&stack, 1)
push(&stack, 2)
push(&stack, 3)
int gv = 0
pop(&stack, &gv)
printf("%d\n", gv)
system("PAUSE")
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)