Say I push the values 1,2,3,4 onto a stack. Then popping them, they will come back in the order 4,3,2,1. If I push the values 1,2,3,4 onto a queue then removing from a queue results in 1,2,3,4. However if I push them onto the queue in reverse order, 4,3,2,1, and then pop them from the queue they come out in the same order as a stack. So a queue with its inputs reversed looks a lot like a stack. Are there any formal methods for proving this?
Asked
Active
Viewed 99 times
0
-
How will you get the inputs in reverse order? Hint: you'll need a stack. An alternative, that makes more sense, is to consider implementing a queue using two stacks. – Dave Clarke Jul 09 '17 at 05:29
-
@DaveClarke The questions about the existance of formal methods for showing the equivalence between data structures and algorithms. The example in the question is meant to illustrate a scenario where this equivalence is apparent. – sashang Jul 09 '17 at 05:47
-
"and then pop them from the queue..." do you mean dequeue? – ryan Jul 09 '17 at 07:02
-
I'm not sure there's a need for a proof. The fact that LIFO is the reverse of FIFO is an self-evident in their definitions, e.g. by definition LIFO would be the reverse of FIFO and vice versa. – ryan Jul 09 '17 at 07:16
-
@ryan in this case yes, in other cases it may not be obvious. – sashang Jul 09 '17 at 10:44
-
3"Are there any formal methods for proving this?" It's not at all clear what "this" is. Have you tried clearly specifying what property you're trying to prove? In particular, while it's immediate from the definition that things are removed from a stack in the opposite order to a queue, both data structures allow you to interleave adding and removing data, and stacks and queues behave very differently when you do that. – David Richerby Jul 09 '17 at 12:44
-
I know what you were trying to ask. I was merely suggesting that your example was pretty poor and offered a better one. – Dave Clarke Jul 09 '17 at 17:23