用c语言编写一个程序实现顺序栈的初始化,出栈和入栈。急需,谢谢
1个回答
展开全部
#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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询