Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() (int, error) {
	n, err := strconv.Atoi(nextString())
	return n, err
}

func main() {
	scanner.Split(bufio.ScanWords)

	stack := []int{}
	for {
		n, err := nextInt()
		if err != nil {
			break
		}

		if n == 0 { // pop
			fmt.Println(stack[len(stack)-1])
			stack = stack[0:(len(stack) - 1)]
		} else { // push
			stack = append(stack, n)
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <cmath>
#include <iomanip>

int main();


int main(){
	std::stack<int> input;		//入力される数（車両番号）を格納する
	std::vector<int> output;	//出力される車両番号
	int dummy = 0;				//ダミーナンバー
	int count = 0;

	while (count < 8){
		std::cin >> dummy;
		count++;
	//while (std::cin >> dummy){
		if ((dummy == 0) && (!input.empty())){
			output.push_back(input.top());
			input.pop();
		}
		else if((1 <= dummy) && (dummy <= 10)){
			input.push(dummy);
		}
		else{
			//何もしない
		}
	}

	for (int i = 0; i < output.size(); i++){
		std::cout << output[i] << std::endl;
	}

	//while (1);
	return 0;
}

C++:  /*
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.

We can simulate the movement (comings and goings) of the cars as follow:

An entry of a car is represented by its number.
An exit of a car is represented by 0
For example, a sequence

1 6 0 8 10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.

Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
 */
 
#include <iostream>
#include <vector>

using namespace std;

int main() {
	vector<int> cars, ans;
	int temp, i=-1, s=0, m;
	while (cin >> temp) {
		if (temp != 0) {
			cars.push_back(temp);
			i++;
		} else {
			m = i-s;
			ans.push_back(cars.at(m));
			s++;
		}
	}
	for (int j=0; j<ans.size(); j++)
		cout << ans[j] << endl;
	return 0;
}

C++: #include<iostream>
#include<vector>

using namespace std;

int main()
{
int a;
vector<int> array;
for(;cin >> a;)
{
if(a==0)
{
cout << array[array.size()-1] << endl;
array.pop();
}
else
array.push_back(a);
}
}

C++: #include<cstdio>
#include<iostream>
using namespace std;

int main()
{
    int train[10]={0},n,count=0,outTrain[10],trainCount=0;
    while(scanf("%d",&n)!=EOF){
        if(n){
            for(int i=count ; i!=0 ; i--){
                train[i]=train[i-1];
            }
            train[0]=n;
            count++;
        }
        else{
            outTrain[trainCount]=train[0];
            for(int i=0 ; i<count ; i++){
                train[i]=train[i+1];
            }
            train[count]=0;
            count--;
            trainCount++;
        }
    }
    for(int i=0 ; i<trainCount ; i++){
        cout << outTrain[i] << '\n';
    }
    return 0;
}

C++: #include<iostream>
#include<stack>
#include<queue>
using namespace std;

int main(){
	int n;
	stack<int> stk;
	queue<int> que;
	while(1){
		cin>>n;
		if(n==0){
			que.push(stk.top());
			stk.pop();
		}else{
			stk.push(n);
		}
		if(stk.empty())break;
	}
	while(!que.empty()){
		cout<<que.front()<<endl;
		que.pop();
	}
	return 0;
}

C++: #define scanf_s scanf
#include <stdio.h>
#include <string>
#include <iostream>
#include <math.h>
using namespace std;
#define MAX 100
int main(void)
{
	int w, n, now = 0, cou = 0;
	int list[MAX] = { 0 };
	int li[MAX] = { 0 };
	do{	
		scanf_s("%d", &n);
		if (n != 0) { list[now] = n; ++now; }
		else { --now; li[cou] = list[now]; ++cou; }
	} while (now != 0);
	
	for (int i = 0; i < cou; ++i) {
		printf("%d\n", li[i]);
	}

}

C++: #include <algorithm>                                                                    
#include <cstdlib>                                                                      
#include <iostream>                                                                     
#include <stack>                                                                        
                                                                                        
using namespace std;                                                                    
                                                                                        
int main() {                                                                            
  stack<int> cars;                                                                      
  const int kNumCars = 10;                                                              
  int num_entered_cars = 0;                                                             
                                                                                        
  do {
    int car;                                                                            
    cin >> car;                                                                         
    if (car == 0) {
      cout << cars.top() << endl;
      cars.pop();
    } else {
      ++num_entered_cars;
      cars.push(car);
    }
  } while (!cars.empty());

  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
int main(void){
    int j=0;
	int k;
	int b[1000]={0};
	stack<int> a;
	
	while(cin>>k){



        if(k==0){
            cout<<a.top()<<endl;

            a.pop();
        }
        else a.push(k);
	}
	/*for(int i=0;i<j;i++){
        cout<<b[i]<<endl;
	}
*/

	return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

int main() {
int n;
stack<int> si;
while (cin >> n){
	if (n == 0){
		cout<<si.top()<<endl;
		si.pop();
	}
	else{
		si.push(n);
	}
}
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <queue>
#include <map>
#include <fstream>
#include <array>
#include <utility>
#include <functional>
#include <stack>

using namespace std;
typedef long long Int;
#define REP(i , x) for(int i = 0 ; i < x ; ++i)

typedef pair<int, int> P;



int main()
{
	stack<int> st;

	int n;
	while (cin >> n){
		if (n){
			st.push(n);
		}
		else{
			cout << st.top() << endl;
			st.pop();
		}
	}

	return 0;
}