栈的c语言实现
#include <stdio.h>
#include <stdbool.h>
#define SIZE 512
int stack[SIZE];
int top;
void push(int data)
{
stack[top++] = data;
}
int pop()
{
return stack[--top]; // top永远指向栈顶元素的下一个位置
}
bool empty()
{
return top == 0;
}
bool full()
{
return top == SIZE;
}
int main(void)
{
push(1);
push(2);
push(3);
push(4);
while (!empty())
{
printf("%d\n", pop());
}
return 0;
}
运行结果: