栈
#include <iostream.h>
const int max = 5;
float num[max];
int top = 0;
void push(float x)
{
if (top==max){return;}
num[top] = x;//
top++;
}
void pop()
{
top--;
// if (top<0){return 0;};
cout<< num[top];
//top--;
}
void main()
{
float x;
for (int j=0;j<max;j++)//入栈
{
cin>>x;
push(x);
cout<<"top"<<top<<endl;
}
for(int i=0;i<max;i++)//出栈
pop();
}
1 入栈:判断是否满,若不满,栈顶指针加1,再入栈。
2 出栈: 判断是否为空,若不空,先返回栈顶元素,再将栈顶指针减1。