Problem:
Switching Railroad Cars   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. Input car number
car number or 0
car number or 0
  .
  .
  .
car number or 0 The number of input lines is less than or equal to 100. Output For each 0, print the car number. Sample Input 1
6
0
8
10
0
0
0 Output for the Sample Input 6
10
8
1

Solution (Python):
import sys

def run():
    cars = []
    for _n in sys.stdin:
        n = int(_n)
        if n == 0:
            print(cars.pop())
        else:
            cars.append(n)

if __name__ == '__main__':
    run()

Solution (Python):
counter = -1
train = []
while True:
	try:
		x = int(input())
	except:
		break
	if x == 0 and len(train) != 0:
		print(train[counter])
		train.remove(train[counter])
		counter -= 1
	else:
		train.append(x)
		counter += 1

Solution (Python):
train = []
while True:
    try:
        n = int(raw_input())
        if n == 0:
            print train.pop()
        else:
            train.append(n)
    except:
        break

Solution (Python):
st = []
while 1:
    try:
        x = int(input())
        st.append(x) if x != 0 else print(st.pop())
    except EOFError:
        break

Solution (Python):

stack = []

while True:
    try:
        data = int(input())
        if data == 0:
            print stack.pop()
        else:
            stack.append(data)
    except:
        break

Solution (Python):


pile = []

while True:
    try:
        num = int(input())
        if num != 0:
            pile.append(num)
        else:
            print(pile[-1])
            pile.pop(-1)
    except:
        break

Solution (Python):



import sys
lis = []
for s in sys.stdin:
  d = int(s)
  if d == 0:
    print lis[-1]
    lis.pop()
  else:
    lis.append(d)

Solution (Python):
lis=[]
cnt=0

while True:
    try:
        n=int(input())
        if n:
            cnt+=1
            lis.append(n)
        else:
            print(lis.pop(cnt-1))
            cnt-=1

    except:
        break

Solution (Python):
import sys

cars = []
for line in sys.stdin:
    car = int(line)
    if car:
        cars.append(car)
    else:
        print(cars.pop())

Solution (Python):
stack = list()
while (True):
    try:
        s = int(input())
    except EOFError:
        break;
    if s == 0:
        print(stack.pop())
    else:
        stack.append(s)

Solution (Python):
ls = []
while 1:
    try:
        n = int(raw_input())
    except EOFError:
        break
    if n!=0:
        ls.append(n)
    else:
        print ls[-1]
        del ls[-1]

Solution (Python):
import sys
Rail = []
for S in sys.stdin:
    S=int(S)
    if S!=0:
        Rail.append(S)
    else:
        print Rail.pop()

Solution (Python):
in_ = []
out = []
try:
	while 1:
		in_.append(raw_input())
		if in_[-1] == "0":
			in_.pop()
			out.append(in_.pop())
except:
	pass

for i in out:
	print(i)

Solution (Python):
ls = []
while True:
    try:
        n = int(raw_input())
    except EOFError:
        break
    if n != 0:
    	ls.append(n)
    else:
    	print ls.pop()

Solution (Python):
import sys
s=[]
for l in sys.stdin:
	n=int(l)
	if n==0:print s.pop()
	else:s.append(n)

Solution (Python):
import sys
s=[]
for l in sys.stdin:
	n=int(l)
	if n==0:print s.pop()
	else:s.append(n)

Solution (Python):
import sys
s=[]
for l in sys.stdin:
	n=int(l)
	if n==0:print s.pop()
	else:s.append(n)

Solution (Python):
import sys
s=[]
for n in map(int,sys.stdin):
	if n==0:print s.pop()
	else:s.append(n)

Solution (Python):
def get_input():
    while True:
        try:
            yield raw_input()
        except EOFError:
            break

num_lis = list(get_input())

train_lis = []
for num in num_lis:
    if num == '0':
        print(train_lis[0])
        del train_lis[0]
    else:
        train_lis.insert(0, num)

Solution (Python):



import sys
import math

railroad = []
outCar = []

for data in sys.stdin:
    data = int(data)
    if data != 0:
        railroad.append(data)
    else:
        outCar.append(railroad.pop())

for data in outCar:
    print(data)

Solution (Python):
import sys
cars = []
out_cars = []
cars_size = 0
for line in sys.stdin:
    n = int(line)
    if n == 0:
        out_cars.append(cars.pop(cars_size - 1))
        cars_size -= 1
    else:
        cars.append(n)
        cars_size += 1
[print(car) for car in out_cars]

Solution (Python):



import sys


def main():
    stack = []
    for a in sys.stdin.read().split("\n"):
        if a == "0":
            print(stack.pop())
        else:
            stack.append(a)

if __name__ == "__main__":
    main()

Solution (Python):


def main():
    LIST=[]
    while True:
        try:
            IN=int(input())
            if IN==0:
                print(LIST[len(LIST)-1])
                del LIST[len(LIST)-1]
            else:
                LIST.append(IN)

        except:
            break
            
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys
l = []
for i in sys.stdin.readlines():
  if int(i) == 0:
    print(l.pop())
  else:
    l.append(int(i))

Solution (Python):
stack = []
while True:
    try:
        n = int(input())
    except:
        break
    stack.append(n) if n else print(stack.pop())

Solution (Python):
res = []
while True:
	try:
		n = input()
		if n == 0:
			print res[len(res)-1]
			res = res[:-1]
		else:
			res.append(n)
	except EOFError:
		break

Solution (Python):
import sys
x=[]
u=[]
for line in sys.stdin.readlines():
    y=int(line)
    if(y!=0):
        x.append(y)
    else:
         u.append(x.pop(-1))
for i in range(len(u)):
    print(u[i])

Solution (Python):
import sys

lines = sys.stdin.readlines()
queue = []
for line in lines:
    if int(line)>0:
        queue.append(int(line))
    elif int(line)==0:
        print (queue[-1])
        queue.pop(-1)

Solution (Python):
garage = []
while True:
    try:
        car = int(input())
    except:
        break
    if car == 0:
        print(garage.pop())
    else:
        garage.append(car)

Solution (Python):
try:
  rail = []
  while True:
    n = int(raw_input())
    if n != 0:
      rail.append(n)
    else:
      print rail.pop()
except:
  pass

Solution (Python):
import sys
carslist = []
anslist = []
for line in sys.stdin:
    n = int(line)
    if n == 0:
        anslist.append(carslist.pop())
    else:
        carslist.append(n)
for ans in anslist:
    print ans

Solution (Python):
import sys
cars = []
for line in sys.stdin:
    car = int(line)
    if car:
        cars.append(car)
    else:
        print(cars.pop())

Solution (Python):
import sys
stk = []
ans = []
for i in sys.stdin:
    if(int(i)==0):
        ans.append(stk.pop())
    else:
        stk.append(int(i))
print('\n'.join(map(str,ans)))

Solution (Python):


stac, car_in, ans = [], [], []
while True:
    try:
        stac.append(input())
    except:
        break

z = len(stac)
for i in xrange(z):
    if stac[i] != 0:
        car_in.insert(0,stac[i])
    elif stac[i] == 0:
        ans.append(car_in[0])
        del car_in[0]

for i in xrange(len(ans)):
    print ans[i]

Solution (Python):
A=[]
while True:
	try:
		num=int(input())
		if num==0:
			print(A[-1])
			A.pop()
		else:
			A.append(num)
	except EOFError:
		break

Solution (Python):
import sys
stack = []
for line in sys.stdin:
    a = int(line)
    if a == 0:
        print(stack.pop())
    else:
        stack.append(a)

Solution (Python):


import sys

stack = []
for line in sys.stdin:
    n = int(line)
    if n == 0:
        print stack.pop()
    else:
        stack.append(n)

Solution (Python):
import sys
orders = sys.stdin.readlines()
stack = []
for order in orders:
    if order == '0\n':
        print(stack.pop(), end = '')
    else:
        stack.append(order)

Solution (Python):
li = []
while True:
    try:
        x = int(input())
    except:
        break

    if x == 0:
        print(li.pop())
    else:
        li.append(x)

Solution (Python):
train=[]
while 1:
    try:
        n=input()
        if n==0:
            print(train.pop())
        else:
            train.append(n)
    except:
        break

Solution (Python):
a = []
while True:
    try:
        s = int(input())
    except:
        break

    if s != 0:
        a.append(s)
    else:
        print(a.pop())

Solution (Python):
import sys

lst = [int(line) for line in sys.stdin.readlines()]

queue = []

for num in lst:
    if num != 0:
        queue.append(num)
    else:
        print(queue[-1])
        del queue[-1]

Solution (Python):
try:
    l = []
    while 1:
        d = int(input())
        if d:
            l.append(d)
        else:
            print(l.pop())
except:
    pass

Solution (Python):
a = [] 
while True: 
  try: 
    i = int(input()) 
  except: 
    break 
  if i == 0: 
    c = a.pop() 
    print(c) 
  else: 
    a.append(i)

Solution (Python):
import sys

stack = []
for i in sys.stdin:
    try:
        a = int(i)
        if a > 0:
            stack.append(a)
        else:
            print stack.pop()
    except:
        pass

Solution (Python):
stack = []

while True:

  try:
    s = int(raw_input())

    if s == 0:
      print stack.pop()

    else:
      stack.append(s)

  except EOFError:
    break

Solution (Python):
stack = list()
while True:
	try:
		num = eval(input())
		if num != 0:
			stack.append(num)
		else:
			print(stack.pop())
	except EOFError:
		break

Solution (Python):
cars = []
while True:
    try:
        num = int(input())
    except:
        break
    if num == 0:
        print(cars[-1])
        cars[-1:] = []
    else:
        cars.append(num)

Solution (Python):
import sys

a = []
for i in sys.stdin.readlines():
    n = i[:-1]
    if n == '0':
        print(a.pop())
    else:
        a.append(n)

Solution (Python):
rail = []
while 1:
    try:
        n = input()
        if n > 0: rail.append(n)
        else: print rail.pop()
    except:
        break

Solution (Python):
import sys
import sys

a = []
for line in sys.stdin:
    a.append(line)

lis=[]
for n in a:
    inp=int(n)
    if inp==0:
        print lis[-1]
        del lis[-1]
    else:
        lis.append(inp)

Solution (Python):

L=[]
while True:
    try:
        n=int(input())
        if n==0:
            print(L[-1])
            L.pop()
        else:
            L.append(n)
    except EOFError:
        break

Solution (Python):
import sys

stack = []
for line in sys.stdin:
    n = int(line)
    if n == 0:
        print(stack.pop())
    else:
        stack.append(n)

Solution (Python):
import sys
s=[]
while True:
    try:
        n=int(input())
    except:
        sys.exit()
    if n==0:
        print(s.pop())
    else:
        s.append(n)

Solution (Python):
import sys
num_li = []
for line in sys.stdin:
    if int(line)==0: print(num_li.pop())
    else: num_li.append(int(line))

Solution (Python):
import sys

s = []
for line in sys.stdin:
    if line.rstrip() == '0':
        print s.pop()
    else:
        s.append(line.rstrip())

Solution (Python):
stack = []
while True:
    try:
        n = int(input())
    except:
        break
    if n == 0:
        print(stack.pop())
    else:
        stack.append(n)

Solution (Python):
Num = []
while True:
    try:
        n = int(input())
        if n != 0:
            Num.append(n)
        elif n == 0:
            print(Num.pop())
    except EOFError:
        break

Solution (Python):
s = []
while True:
    try:
        n = int(input())
    except EOFError:
        break
    if n == 0:
        print(s.pop())
    else:
        s.append(n)

Solution (Python):
memory=[]
while True:
  try:
    n=int(input())
  except EOFError:
    break
  if n==0:
    print(memory.pop())
  else:
    memory.append(n)

Solution (Python):
import sys
from collections import deque


if __name__ == '__main__':
    s = deque()  
    
    for line in sys.stdin:
        if line.strip() == '0':
            print(s.pop())
        else:
            s.append(line.strip())

Solution (Python):
cars = []
gone = []
while True:
    try:
        c = int(input())
        if c != 0:
            cars.append(c)
        else:
            gone.append(cars.pop())
    except:
        break
print('\n'.join(map(str, gone)))

Solution (Python):



stack = []

while True:
    try:
        a = int(input())
        if a == 0:
            print(stack[-1])
            stack.pop()
        else:
            stack.append(a)
            
    except EOFError:
        break

Solution (Python):
tracks=[]
while True:
    try:
        s=int(input())
        if s==0:
            print(tracks.pop())
        else:
            tracks.append(s)
    except:
        break

Solution (Python):
import sys
a=[]
stack=[]
for line in sys.stdin:
    a.append(int(line))
for i in range(len(a)):
    if a[i]==0:
        print(stack[-1])
        del stack[-1]
    else:stack.append(a[i])

Solution (Python):
a=[]
while 1:
    try:n=int(input())
    except:break
    if n:a.append(n)
    else:print(a.pop())

Solution (Python):

array = []
while True:
    try:
        n = int(input())
        array.append(n)
    except EOFError:
        break
data = []
for i in range(len(array)):
    if array[i] == 0:
        elm = data.pop()
        print(elm)
    else:
        data.append(array[i])

Solution (Python):
import sys

cars = []

while True:
  n = sys.stdin.readline()
  if not n: break
  n = int(n.rstrip())
  if n == 0:
    print(cars.pop())
  else:
    cars.append(n)

Solution (Python):
import sys

arr = []
for line in sys.stdin:
    n = int(line)
    if n == 0:
        print(arr.pop())
    else:
        arr.append(n)

Solution (Python):
array = []
array2 = []
import sys
for i in sys.stdin.readlines():
    n = i.rstrip()
    if n == "0":
        array2.append(array[-1])
        array.pop(-1)
    else:
        array.append(n)
for i in range(len(array2)):
    print(array2[i])

Solution (Python):
buf = []
while True:
    try:
        car = int(input())
    except:
        break
    if car:
        buf.append(car)
    else:
        print(buf.pop())

Solution (Python):
import sys
import math as mas

a=[]
for t in sys.stdin:
	i=int(t)
	if i:a.append(i)
	else:
		print(a[-1])
		a.pop()

Solution (Python):


import sys
import os
import math

stack = []
for s in sys.stdin:
    v = int(s)
    if v == 0:
        print(stack.pop())
    else:
        stack.append(v)

Solution (Python):
from sys import stdin
from collections import deque
box = deque()
for _, train in enumerate(stdin) :
    if train.strip() != '0' : box.append(train.strip())
    else : print(box.pop())

Solution (Python):
list = []
while True:
    try:
        num = int(input())
        if num == 0:
            print(list.pop())
        else:
            list.append(num)
    except:
        break

Solution (Python):
import sys


shako = []

s = sys.stdin.read().strip().split("\n")

for i in s:
    if i != "0":
        
        shako.append(i)
        
    else:
        print(shako.pop())

Solution (Python):
import sys


shako = []

s = sys.stdin.read().strip().split("\n")

for i in s:
    if i != "0":
        
        shako.append(i)
        
    else:
        print(shako.pop())

Solution (Python):
import sys
l = []
for line in sys.stdin:
    v = int(line)
    if v != 0:
        l.append(v)
    else:
        print(l.pop())

Solution (Python):
from sys import stdin
stack = []
for line in stdin:
    line = int(line)
    if line == 0:
        print(stack.pop(0))
    else:
        stack.insert(0,line)

Solution (Python):
import sys

cars = []
for i in sys.stdin:
    if int(i) == 0:
        print(cars.pop())
    else:
        cars.append(int(i))

Solution (Python):
import fileinput
a = []
for n in fileinput.input():
    n = int(n)
    if n == 0:
        print(a.pop())
    else:
        a.append(n)

Solution (Python):
import sys
cars = []
num = -1

for user in sys.stdin:
    i = int(user)
    if i == 0:
        print(cars[num])
        cars.pop(num)
        num -= 1
    else:
        cars.append(i)
        num += 1

Solution (Python):
tracks = []
while True:
    try:
        c_num = int(input())
    except:
        break
    if c_num == 0:
        print(tracks.pop())
    else:
        tracks.append(c_num)

Solution (Python):
import sys
def main():
    a = []
    for line in sys.stdin:
        hoge = int(line)
        if hoge != 0:
            a.append(hoge)
        elif hoge == 0:
            print(a.pop(-1))


if __name__ == "__main__":
    main()

Solution (Python):


import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


stack = []
for line in sys.stdin:
    n = int(line)
    if n == 0:
        print(stack.pop())
    else:
        stack.append(n)

Solution (Python):
stack=[]
i=0
x=int(input())
stack.append(x)
while 1:
    try:
        x=int(input())
        if (x==0):
            print(stack[i])
            stack.pop()
            i-=1
        else:
            stack.append(x)
            i+=1
    except EOFError:
        break

Solution (Python):
import sys
data = []
for l in sys.stdin:
    l = int(l)
    if l == 0:
        print(data.pop())
    else:
        data.append(l)

Solution (Python):
import sys

d = sys.stdin.readline().rstrip()
stack = list()
while d:
    if d != '0':
        stack.append(d)
    else:
        print(stack.pop())
    d = sys.stdin.readline().rstrip()

Solution (Python):
import sys
if __name__ == '__main__':
    a = []
    for tmpLine in sys.stdin:
        x = (int)(tmpLine)
        if x != 0:
            a.append(x)
        else:
            print(a.pop())

Solution (Python):
t = 0
numbers = []
while t == 0:
    try:
        number = int(input())
    except:
        break
    else:
        if number == 0:
            print(numbers[-1])
            numbers.pop(-1)
        else:
            numbers.append(number)

Solution (Python):



if __name__ == '__main__':
    train_list = []
    while True:
        try:
            n = int(input())
        except EOFError:
            break
        if n == 0:
            leaving = train_list.pop()
            print(leaving)
        else:
            train_list.append(n)

Solution (Python):


syako = []
try:
    while True:
        number = int(raw_input())
        if number != 0:
            syako.append(number)
        elif number == 0:
            print syako.pop()
except:
    pass

Solution (Python):
arr = []

while True:
    try:
        n = int(input())
    except:
        break

    if n == 0:
        print(arr[-1])
        arr = arr[:-1]
    else:
        arr.append(n)

Solution (Python):
def main():
    _Stack = []
    while True:
        try:
            n = int(input())
            if n == 0:
                print(_Stack.pop())
                continue
            else:
                _Stack.append(n)
        except EOFError:
            break
        
if __name__ == '__main__':
    main()

Solution (Python):
import sys

stack = []

for line in sys.stdin:
  try:
    train = int(line)
    if train == 0:
      print(stack.pop())
    else:
      stack.append(train)
  except:
    break

Solution (Python):
import sys

s = []

for i in map(int, sys.stdin):
    if i:
        s.insert(len(s), i)
    else:
        print(s.pop())

Solution (Python):
i= 0
a,b = [],[]

while True:
    try:
        a.append(int(raw_input()))

        if a[i] == 0:
            b.append(a[i-1])
            a.pop()
            print a[i-1]
            a.pop()
            i -=1
        else:
            i +=1

    except:
        break

Solution (Python):
stack = []
while True:
    try:
        car = int(input())
        if car == 0:
            print(stack.pop())
        else:
            stack.append(car)
    except:
        break

Solution (Python):


class Stack(object):
	maxnum = 100
	array = []
	for i in range(100):
		array.append(0)
	index = 0
	def push(self, data):
		self.array[self.index] = data
		self.index += 1
	def pop(self):
		print(self.array[self.index-1])
		self.index -= 1

road = Stack()
while True:
	try:
		num = int(raw_input())
		if num != 0:
			road.push(num)
		else:
			road.pop()
	except EOFError:
		break

Solution (Python):
stack = []
while True:
    try:
        num = int(input())
    except:
        break
    if num == 0:
        print(stack.pop())
    else:
        stack.append(num)

Solution (Python):
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

N = list(get_input())
stack = []
for l in range(len(N)):
    a = int(N[l])
    if a == 0:
        print(stack.pop())
        
    else:
        
        stack.append(a)

Solution (Python):
import sys
a=[]
for e in map(int,sys.stdin):
 a.append(e)if e else print(a.pop())

Solution (Python):
a=[]
while 1:
 try:n=int(input());a.append(n)if n else print(a.pop())
 except:break

Solution (Python):
import sys
a=[]
for e in map(int,sys.stdin):a.append(e)if e else print(a.pop())

Solution (Python):
c=[]
s=[]
e=[]
while True:
    try:
        t=input()
        if not t:break
    except:break
    c.append(int(t))

for i in c:
    if i==0:
        e.append(s.pop(-1))
    else:
        s.append(i)
[print(i) for i in e]

Solution (Python):
st = []
while True:
  try:
    n = int(input())
    if n == 0:
      print(st.pop())
    else:
      st.append(n)
  except EOFError:
    break

Solution (Python):
li = []
while True:
    try:
        x = int(input())
    except:
        break
 
    if x == 0:
        print(li.pop())
    else:
        li.append(x)

Solution (Python):
import sys
a=[int(i)for i in sys.stdin]

b=[]

for j in a:
	if j == 0:
		print(b.pop())
	else:
		b.append(j)

Solution (Python):
import sys
a=[int(i)for i in sys.stdin]

b=[]
for j in a:
	if j == 0:
		print(b.pop())
	else:
		b.append(j)

Solution (Python):
import sys
s = []
[print(s.pop()) if int(e) == 0 else s.append(int(e)) for e in sys.stdin]

Solution (Python):
import sys
queue = []
for line in sys.stdin:
    n = int(line)
    if n != 0:
        queue.append(n)
    else:
        print(queue.pop())

Solution (Python):
import sys
l=[]
for line in sys.stdin:
    o=int(line)
    if not o:
        print(l.pop())
    else:
        l.append(o)

Solution (Python):



class Stack:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[len(self.items)-1]

    def size(self):
        return len(self.items)

s = Stack()
while True:
    try:
        a = int(input())
        if a > 0:
            s.push(a)
        else:	
            print(s.pop())
    except EOFError:
        break

Solution (Python):
import sys

if __name__ == '__main__':
    stack = []
    for line in sys.stdin:
        n = int(line)
        if n == 0:
            print(stack.pop(len(stack) - 1))
        else:
            stack.append(n)

Solution (Python):
import sys

lst = []
for line in sys.stdin:
    if line:
        num = line.strip()
        if num == "0":
            print(str(lst.pop()) + "")
        else:
            lst.append(num)
sys.exit(0)

Solution (Python):
import sys

lst = []
for line in sys.stdin:

    num = line.strip()
    if num == "0":
        print(str(lst.pop()) + "")
    else:
        lst.append(num)
sys.exit(0)

Solution (Python):
import sys

lst = []
for line in sys.stdin:
    num = line.strip()
    if num == "0":
        print(lst.pop() + "")
    else:
        lst.append(num)
sys.exit(0)

Solution (Python):
import sys

lst = []
for line in sys.stdin:
    num = line.strip()
    if num == "0":
        print lst.pop()
    else:
        lst.append(num)
sys.exit(0)

Solution (Python):
import sys

lst = []
for line in sys.stdin:
    num = line.strip()
    if num == "0":
        print lst.pop()
    else:
        lst.append(num)

Solution (Python):
import sys

lst = []
for line in sys.stdin:
    if line.strip() :
        num = int(line.strip())
        if num == 0:
            print lst.pop()
        else:
            lst.append(num)

Solution (Python):
import sys
def clearSpace(s):
    s.pop()
    return s
list=[]
for i in sys.stdin:
    if i.strip() != '0':
        list.append(i.strip())
    else:
        print list.pop()

Solution (Python):
import sys
list=[]
for i in sys.stdin:
    if i.strip() != '0':
        list.append(i.strip())
    else:
        print list.pop()

Solution (Python):
import sys
stack=[]
for i in sys.stdin:
    try:
        a=int(i)
        if a >0 :
            stack.append(a)
        else :
            print stack.pop()
    except:
        pass

Solution (Python):
tracks = []
while 1:
    try:
        c0 = raw_input()
        if c0 == '':
            break
        c = int(c0)
        if c == 0:
            print tracks.pop()
        else:
            tracks.append(c)
    except EOFError:
        break

Solution (Python):
stack = []

def datasets():
    import sys
    while True:
        s = sys.stdin.readline()
        if len(s) < 2:
            break
        yield int(s)

for n in datasets():
    if n == 0:
        print stack.pop()
    else:
        stack.append(n)

Solution (Python):
from collections import deque
garage = deque()
while True:
    try:
        came = int(raw_input())
        if came == 0:
            print garage.pop()
        else:
            garage.append(came)
    except ValueError:
        break

Solution (Python):
l = []
while True:
	try:
		train = raw_input()
		if train == '0':
			print(l.pop())
		else:
			l.append(train)
	except EOFError:
		break

Solution (Python):
ans = []
while True:
  try:
    a = input()
    if a != 0: ans.append(a)
    elif a == 0: 
      print ans[-1]
      del ans[-1]
  except EOFError: break
  except SyntaxError: continue

Solution (Python):
stack = [];

while True:
 try:
  n = input();
  if (n == 0):
   print stack.pop()
  else:
   stack.append(n);
 except:
  break;

Solution (Python):
stack = [];

while True:
 try:
  n = input();
  if (n == 0):
   print stack.pop()
  else:
   stack.append(n);
 except:
  break;

Solution (Python):
ans = []
while True:
  try:
    a = input()
    if a != 0: ans.append(a)
    elif a == 0: 
      print ans[-1]
      del ans[-1]
  except EOFError: break
  except SyntaxError: continue

Solution (Python):
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin

L = []
for line in stdin:
    if not line.strip():
        continue
    n = int(line)
    if not n:
        print(L.pop())
    else:
        L.append(n)

Solution (Python):
bottom = []
exits = []
while True:
	carnum = raw_input()
	if carnum == '':
		break
	elif carnum == '0':
		exits.append(bottom[0])
		del bottom[0]
		continue
	else:
		bottom.insert(0,int(carnum))
		continue
for val in exits:
	print val

Solution (Python):
import sys






stack = []

for n in sys.stdin:
    n = int(n)
    if n == 0:
        print stack.pop()
    else:
        stack.append(n)

Solution (Python):
list = []
while True:
    try:
        a = int(raw_input()
)
        if a != 0:
            list.append(a)
        else:
            b = list.pop()
            print b

    except EOFError:
        break

Solution (Python):
x=[]
while True:
    try:
        n = input()
        if n==0: print x.pop()
        else: x.append(n)
    except:
        break

Solution (Python):
import sys

s = []

for line in sys.stdin:
    t =	int(line)
    if t == 0:
	print s.pop()
    else:
	s.append(t)

Solution (Python):
stack = []

while True:
    try:
        n = int(raw_input())
        if n == 0:
            print stack.pop()
        else:
            stack.append(n)
    except EOFError:
        break

Solution (Python):


import sys

stack = []

for line in sys.stdin:
	

	num = int(line)
	if num == 0:
		pop = stack.pop()
		
		print(str(pop))
	else:
		stack.append(num)

Solution (Python):
t = []
while 1:
    try:
        a = input()
    except:
        break
    if a == 0:
        print(t[-1])
        t = t[:-1]
    else:
        t.append(a)

Solution (Python):
s = []
while 1:
    try:
        n = input()
        if n==0: print s.pop()
        else: s.append(n)
    except EOFError:
        break;

Solution (Python):


import sys


lineNumber = 0

for line in sys.stdin.readlines():
    lineNumber += 1

    
    if lineNumber == 1:
        cars = []
    
    
    List = map(int, line.strip().split())

    n = List[0]
    if n == 0: print cars.pop()
    else     : cars.append(n)

Solution (Python):
stuck = []
while True:
    try:
        x = int(raw_input())
        if x == 0:
            go = stuck.pop()
            print go
        else:
            stuck.append(x)
    except:
        break

Solution (Python):
stack = []
while True:
    try:
        num = input()
        if num == 0:
            print stack.pop()
        else:
            stack.append(num)
    except EOFError:
        break

Solution (Python):
import sys
x=[]
for n in map(int,sys.stdin):
  if n:x.append(n)
  else:print x.pop()

Solution (Python):
import sys
x=[]
for n in map(int, sys.stdin):
  if n: x.append(n)
  else: print x.pop()

Solution (Python):
import sys
storage = []
for car in sys.stdin:
    car = int(car)
    if car > 0:
        storage.append(car)
    elif car == 0:
        print storage.pop()
while storage:
    print storage.pop()

Solution (Python):
cars = []
ans = []

while True:
	try:
		n = int(raw_input())
		if n == 0:
			ans.append(cars.pop())
		else:
			cars.append(n)
	except EOFError:
		for car in ans:
			print car
		break

Solution (Python):
L = []
while 1:
    try:
        n = input()
        if n == 0:
            print L.pop()
        else:
            L.append(n)
    except:
        break

Solution (Python):


import sys
i = 0
inn=[]
for i in sys.stdin:
    i=int(i)
    if i==0 and len(inn)>0:
        print inn[-1]
        inn.pop()
    elif i==0 and len(inn)==0:
        sys.exit()
    elif i!=0:
        inn.append(i)
for i in inn:
    print i

Solution (Python):

try:
    a = []
    while True:
        p = int(raw_input())
        if p == 0:
            print a.pop()
        else:
            a.append(p)
except:
    pass

Solution (Python):
import sys

stack = []
for n in sys.stdin:
  if int(n) == 0:
    print '%d'%int(stack.pop())
  else:
    stack.append(n)

Solution (Python):
stack = []

while True:
    try:
        n = int(input())
    except:
        break

    if n:
        stack.append(n)
    else:
        print(stack.pop())

Solution (Python):
DE = []
while True:
    try:
        N = int(input())
    except:
        break
    if N > 0:
        DE.insert(0,N)
    if N==0:
        print(DE[0])
        DE.pop(0)

Solution (Python):
train = []
while True:
    try:
        tmp = int(input())
        if tmp:
            train.append(tmp)
        else:
            print(train.pop())
    except:
        break

Solution (Python):
lst = []
stack = []
while True :
    try :
        lst.append(int(input()))
    except EOFError :
        break
for i in lst :
    if i == 0 :
        print(stack.pop(-1))
    else :
        stack.append(i)