When integers join the queue
Python 2, 56 53 50 bytes
q=[]
for i in input():q=[[i]+q,q[:i]][i<0]
print q
Try it Online!
Dequeue is -1
. This trick allows easy pythonic slicing of the queue.
Mathematica, 102 bytes
Definitely not the shortest solution, but I couldn't resist because it's kind of perverse.
r=Reverse@{##}&
a_~f~b___:=b
f[a_,b___,]:=b
ToExpression[{"r[","f["~Table~StringCount[#,"]"],#}<>"]"]&
After some helper functions, this defines a pure function that takes a string as input: in the string, numbers are separated by commas (whitespace is optional); the dequeue character is "]"
; and the list does not have delimiters in the front or back. For instance, the first example in the OP would be input as the string "45,],],37,20,],97,],85"
. The output of the function is a list of numbers.
The function counts how many dequeues "]"
are in the input string, appends that many copies of "f["
to the front of the string, and then surrounds the whole thing by "r[...]"
. In the example above, this produces "r[f[f[f[f[45,],],37,20,],97,],85]"
; notice the brackets are balanced.
Then, ToExpression
interprets the resulting string as a piece of Mathematica code and executes it. The function f
is conveniently defined to retain all its arguments except the first (and also ignores trailing commas; this is necessary to handle dequeueing empty queues anyway), and r
converts the resulting sequence of numbers into a list of numbers in the right order.
Retina, 30 bytes
1+`\d+,(.*?)X,?|^X,
$1
O^$`\d+
Try it online!
Repeatedly removes the first number that's (not necessarily immediately) followed by an X
together with that X
, or an X
at the beginning of the string. Then reverses the remaining numbers.