Implementing a queue with a stack structure

Basic idea: define two stacks, a push1 stack and a pop1 stack. Every time a number is added, it is added to the push1 stack, and every time a number is fetched, it is taken from the pop1 stack. Another important operation is the countdown operation. It is to pour the number in the push1 stack into the pop1 stack. Using the nature of the stack (first in, last out), the number in push1 can be reversed after entering pop1 (the original number in push1: 5 4 3 2 1 After the countdown operation is completed, there is no number in push1, and the number in pop1: 1 2 3 4 5). For the countdown operation to occur, the following two conditions must be met:

Condition 1: If you want to count down, you must pour all the numbers in the push1 stack into pop1, and you cannot leave the number.

Condition 2: If you want to count down, you must make sure that there is no number in the pop1 stack before the countdown.

As long as these two conditions are met at the same time, the reciprocal behavior can occur.



code show as below:

#include<bits/stdc++.h>
using namespace std;
stack< int >push1,pop1; //The put number is always placed in the push1 stack, and the output number is always output
string in the pop1 stack a;
int n;
void dao() / / Pour the number in push1 into pop1, using the nature of the stack, first in and last out, just to realize the number switching. Example: Originally 1 2 3 4 5, after switching it becomes: 5 4 3 2 1. If you want to output, you can just output 5, which is in line with the first-in, first-out nature of the queue.
{         //The behavior of reversing data must meet two conditions. The first is: if you decide to count down, you must push all the numbers in the push1 stack. Poured into the pop1 stack, all must be poured. The second condition: there must be no number in the pop1 stack before the countdown
    if(!pop1.empty()) //The judgment of the second condition, if there is a number in pop1,
         return directly;
    while (!push1.empty( )) //Make sure all the numbers in push1 are poured into pop1
    {
        pop1.push(push1.top());
        push1.pop();
    }
}
void push(int n)
{
    push1.push(n) ;
    dao();
}
void pop()
{
    if(pop1.empty()) //If there is no number in the pop1 stack, it means that there is no number in the entire queue, and an error is reported to the user
    {
        cout<<"error"<<endl;
        return ;
    }
    cout <<pop1.top()<<endl;; //Output the number on the top of the stack
    pop1.pop();
}
int main()
{
    while (cin>>a)
    {
        if(a=="push")
        {
            cin >>n;
            push(n);
        }
        if(a=="pop")
        {
            dao(); //This place ensures a situation, there is a number in push1, but there is no number in pop1, if this happens, first Pour the number in push1 into pop1 and then perform the pop operation
            pop();
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767744&siteId=291194637