Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few very good reasons to do this.</p> <p>First, some functional programming languages like Haskell, ML, or Lisp support lists as a built-in type. Lists typically are represented as singly, forward-linked lists, which means that they support O(1) prepend but O(n) concatenate. In languages like this, it's extremely easy to make a stack - you just prepend to a list to push and drop the first element to pop. Because of the internal implementation, this runs in O(1) time. If you tried to make a queue using this sort of list, enqueue would take O(n) time because you'd have to add to the end of a singly-linked list that doesn't store a pointer to the last element. On the other hand, if you implement the queue using two stacks (or more; the <a href="http://dl.acm.org/citation.cfm?id=867505" rel="nofollow noreferrer">Hood-Melville queue</a> uses six!), then you can get amortized O(1) enqueue and dequeue even if you only have stacks in your language. Although more advanced data structures have been devised to support purely functional queues and lists (such as the <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.116.4541&amp;rep=rep1&amp;type=pdf" rel="nofollow noreferrer">2-3 finger tree</a>), the two-stack construction is still quite useful in many applications.</p> <p>In addition to this, in some cases you may want to use special stacks to implement the queue to get extra functionality out. For example, you can <a href="https://stackoverflow.com/questions/7134129/stack-with-find-min-find-max-more-efficient-than-on/7134189#7134189">augment a stack data structure</a> to support O(1) find-min/find-max. If you have a stack like this, you can then <a href="https://stackoverflow.com/questions/4802038/implement-a-queue-in-which-push-rear-pop-front-and-get-min-are-all-constan/4802260#4802260">use the two-stack construction to make a queue that also has O(1) find-min/find-max</a>. Trying to solve this problem directly is much harder (check out my <a href="https://stackoverflow.com/questions/4802038/implement-a-queue-in-which-push-rear-pop-front-and-get-min-are-all-constan/4802327#4802327">considerably more complex construction</a> to make a queue with these properties!)</p> <p>Finally, it is interesting from a theoretical perspective to know that a queue can be simulated with two stacks. In computability theory, a <a href="http://en.wikipedia.org/wiki/Pushdown_automaton#Operation" rel="nofollow noreferrer">two-stack pushdown automaton</a> is a theoretical computing device with power equivalent to a Turing machine. A <a href="http://en.wikipedia.org/wiki/Queue_machine" rel="nofollow noreferrer">queue automaton</a> is a similar structure that uses a queue instead of two stacks. Because we know that you can simulate a queue with two stacks, you can immediately prove that the queue automaton is at least as powerful as a Turing machine, and thus that queue automata are Turing-complete.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload