Web Front end Interview series-data structure (two stacks simulate a queue)

Source: Internet
Author: User

One, using two stacks to simulate a queue

Idea One:

1. One stack s1 as the data store, and the other stack S2, as a temporary data store.

2. S1 the data when the team is in the queue

3. Out of the team will S1 pop, and press people S2, and then pop S2 in the top of the data, and then the rest of the data pop s2, and pressure people S1.

Idea two:

1. One stack s1 as the data store, and the other stack S2, as a temporary data store.

2. When the team, determine whether the S1, is empty, if not empty, the data will be pressed into the S1, if it is empty, the data in S2 will be poured into S1, in the data pressure people s1.

3. When the team, Judge S2, whether empty, if not empty, the data directly pops up S2, if it is empty, the data in S1 will be poured into S2, and then pop the data at the top of the S2.

Three ideas:

1. Press the data into the S1 when you queue.

2. When the team is out, determine if S2 is empty, and if not empty, the top element is ejected directly, or if empty, the S1 data is poured into S2 and the top element of the S2 is ejected.

Web Front end Interview series-data structure (two stacks simulate a queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.