Go: package main

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

type Railway struct {
	trains trains
}

type trains []int

func main() {
	var stdin string
	var s = bufio.NewScanner(os.Stdin)

	railway := Railway{}
	for s.Scan() {
		stdin = s.Text()

		n, err := strconv.Atoi(stdin)
		if err != nil {
			continue
		}
		railway.lifo(n)
	}
}

func (r *Railway) lifo(n int) {
	if n == 0 {
		if len(r.trains) == 0 {
			return
		}
		fmt.Println(r.trains[len(r.trains)-1])
		r.trains = r.trains[:len(r.trains)-1]
	} else {
		r.trains = append(r.trains, n)
	}
}

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;
}

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>
#include <algorithm>

using namespace std;

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

C++: #include<bits/stdc++.h>
using namespace std;

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

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

using namespace std;

int main(){
	stack<int> st;
	int car;
	cin >> car;
	st.push(car);
	if(car == 0) return 0;
	while( !st.empty() ){
		int inout;
		cin >> inout;
		if(inout == 0){
			cout << st.top() << endl;
			st.pop();
		}else{
			st.push(inout);
		}
	}
	return 0;
}

C++: #include <stdio.h>
 
int main(void) {
    int A[10];
    int m=0, n;
    while(!feof(stdin)) {
        scanf("%d",&n);
        if(feof(stdin))break;
        if(n==0) {
            m--;
            printf("%d\n",A[m]);
        } else {
            A[m]=n;
            m++;
        }
    }
    return 0;
}

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

std::stack<int> st;
int main() {
	int x = 0;
	while(std::cin >> x) {
		if(x == 0) {
			int y = st.top(); //トップの値を取得
			st.pop(); //トップの値を消去(pop)
			std::cout << y << std::endl;
		} else {
			st.push(x); //トップに値を積む(push)
		}
	}
}