Two stacks implement one queue, two queues implement one stack

Source: Internet
Author: User

Two stacks implement one queue:

The first stack is used for insertion, and the Second stack is used for deletion. The data in the two stacks is queue data, and the data flow is popped up from the first stack, and pushed into the Second stack without data. Therefore, the data capacity must be at least the length of one stack and at most two stacks.

Therefore, the first stack increases and the Second stack decreases.


Two queues implement one stack:

The first stack is used for insertion and deletion, and the Second stack is used to save advanced data. During insertion, data is always inserted from the first queue. If there is data in the first queue during deletion, the advanced data is put in the Second stack. After deletion, the data can be inserted again. Therefore, the data capacity can be at most the length of one 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.