スタックを実装するだけの問題です。
C言語で提出する場合には、1から実装する必要がありますが、
C++ならSTLを使うと実装しなくていいので楽です。
#include <iostream>
#include <stack>
using namespace std;
int main(){
stack<int> st;
int n, a;
while(cin >> n){
if(n!=0){
st.push( n );
}else{
a = st.top();
cout << a << endl;
st.pop();
}
}
}