Jiudu_topic 1512: queue using two stacks

Source: Internet
Author: User

This idea is too difficult to think about ....

/** STR is push, and elements are pushed into the stack through s1.push (); * When STR is pop, S2 is not empty, the top element of the output stack is displayed, and s2.pop () is the top element of the stack; if the element after S2 pop-up is empty and S1 is not empty, push all elements in S1 into S2; * If STR is pop, S2 is empty. If S1 is not empty, push all elements in S1 to S2, and finally output the top elements of the stack and use <span style = "font-family: Arial, Helvetica, sans-serif;"> s2.pop () the top element of the stack is displayed. </span> ;*/
Description: Two stacks are used to implement a queue and push and pop operations of the queue are completed.
The element in the queue is of the int type.
Input:
Each input file contains a test sample.
For each test example, enter N (1 <= n <= 100000) In the first line, which indicates the number of queue operations.
For the next n rows, enter a queue operation for each row:
1. Push X to push an integer x (x> = 0) to the queue)
2. Pop: the number of POPs in the queue.
Output:
For each test case, print the numbers from the pop in the queue in all pop operations. If the queue is empty during the pop operation, print-1.
Sample input:
3
Push 10
Pop
Pop
Sample output:
10
-1
# Include <iostream> # include <string> # include <stack> using namespace STD;/** STR: Push, s1.push (); * When STR is pop, S2 is not empty, s2.pop () is the top element of the output stack. If S2. if S2. if SS1 is not empty, all elements in S1. * If STR is pop, S2, if S1 is not empty, push all elements in S1 to S2, and finally output the top element of the stack; */INT main () {int N; while (CIN> N) {stack <int> S1, S2; int num; string STR; For (INT I = 0; I <n; I ++) {CIN> STR; if (STR = "push") {CIN> num; s1.push (Num);} else if (STR = "pop") {If (! S2.empty () {cout <s2.top () <Endl; s2.pop (); If (s2.empty () {If (! S1.empty () {While (! S1.empty () {s2.push (s1.top (); s1.pop () ;}}} else {If (s1.empty () {cout <-1 <Endl; continue;} else {While (! S1.empty () {s2.push (s1.top (); s1.pop () ;}} cout <s2.top () <Endl; s2.pop ();}}}}} /*************************************** * *********************** problem: 1512 User: hndxztf language: C ++ result: accepted time: 580 MS memory: 1656 kb ************************************** **************************/

Jiudu_topic 1512: queue using two stacks

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.