#include <iostream>
#define STACK_SIZE 20
typedef struct stack
{
unsigned int array[STACK_SIZE];
int top;
} ARRAY_STACK_S;
void init_stack(ARRAY_STACK_S *_stack)
{
_stack->top = -1;
printf("init a queue successfully!\n");
}
int push_stack(ARRAY_STACK_S *_stack, int _element)
{
/* 判断是否是满栈 */
if (_stack->top >= STACK_SIZE){
printf("the stack is full!\n");
return 0;
}else{
/* 栈顶向上移动一位,然后再将数据放进去 */
_stack->array[++_stack->top] = _element;
printf("push stack an element %d\n", _element);
return 1;
}
}
int pop_stack(ARRAY_STACK_S *_stack, int *_element)
{
/* 判断是否是空栈 */
if (-1 == _stack->top){
printf("the stack is empty!\n");
return 0;
}else{
/* 先取出数据,栈顶再下移动一位 */
*_element = _stack->array[_stack->top--];
printf("pop stack an element %d\n", *_element);
}
return 1;
}
int main(int argc, char *argv[])
{
int j;
ARRAY_STACK_S stack;
init_stack(&stack);
for (int i = 0; i < 10; i++){
if (0 == push_stack(&stack, &j)){
break;
}
}
for (int i = 0; i <= 10; i++){
if (0 == pop_stack(&stack, &j)){
break;
}
}
return 0;
}
C语言实现数组栈
猜你喜欢
转载自blog.csdn.net/lala0903/article/details/106485133
今日推荐
周排行