Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • Good point about the size() method. However, in the absence of an O(1) size() method, it's trivial for the stack to keep track of its current size itself. Nothing to stop a one-queue implementation. Commented Jun 18, 2015 at 20:31
  • It all depends on the implementation. If you have a queue, implemented with only forward pointers and pointers to first and last element, you could still write and algorithm which removes an element, saves it to a local variable, adds the element previously in that variable to the same queue until the first element is seen again. This only works if you can uniquely identify an element (e.g. via pointer) and not just something with the same value. O(n) and only uses add() and remove(). Anyway, it is easier to optimize, that to find a reason to actually do that, except thinking about algorithms. Commented Jun 19, 2015 at 15:08