Interview questions: two stacks analog simulation queue queues && two stacks

Two stacks analog queues (leetcode interview questions 09)

Solution: Define Stack s1 and s2, the team put s1, the team then take priority s2, then s1 s2 is all empty Go s2 then take.

S1 suffix corresponding to the discharge current, s2 discharge current prefix.

class CQueue {
public:

    stack<int>s1, s2;
    CQueue() {
    }
    
    void appendTail(int value) {
        s1.push(value);
    }
    
    int deleteHead() {
        if(s1.empty() && s2.empty())  return -1;
        if(s2.empty())
        {
            while(!s1.empty())
            {
                s2.push(s1.top());
                s1.pop();
            }
        }
        int tmp = s2.top();
        s2.pop();
        return tmp;
    }
};

Two analog queue stacks (leetcode225)

Solution: Drawing: the element into the queue A. Stack: A queue is determined whether the number of elements 1, 1 if it is equal, then the queue; if A is more than one element, the element A will be placed in a queue and dequeue this queue B, until the queue a is a left element, then the queue a queue; otherwise, a is empty, the same way until the B to a left a transfer.

Another approach: using a queue, better, direct stack push, pop out of the n-1 prior to team again push, be equivalent to the cycle.

#include<bits/stdc++.h>
using namespace std;

class MyStack {
private:
    queue<int>q1, q2;
public:
    /** Initialize your data structure here. */

    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        q1.push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int ret;
        if(q1.empty() && q2.empty())  return -1;
        int sz = q1.size();
        if(sz == 1)
        {
            ret = q1.front();
            q1.pop();
        }
        else if(sz > 1)
        {
            while(q1.size() > 1)
            {
                q2.push(q1.front());
                q1.pop();
            }
            ret = q1.front();
            q1.pop();
        }
        else
        {
            while(q2.size() > 1)
            {
                q1.push(q2.front());
                q2.pop();
            }
            ret = q2.front();
            q2.pop();
        }
        return ret;
    }

    /** Get the top element. */
    int top() {
        int ret;
        if(q1.empty() && q2.empty())  return -1;
        int sz = q1.size();
        if(sz == 1)
        {
            ret = q1.front();
            //q1.pop();
        }
        else if(sz > 1)
        {
            while(q1.size() > 1)
            {
                q2.push(q1.front());
                q1.pop();
            }
            ret = q1.front();
            //q1.pop();
        }
        else
        {
            while(q2.size() > 1)
            {
                q1.push(q2.front());
                q2.pop();
            }
            RET = q2.front ();
             // q2.pop (); 
            q1.push (q2.front ());   // Q2 to transfer a complete rest, or the order will not 
            q2.pop (); 
        } 
        return RET; 
    } 

    / * * Returns Whether The Stack IS empty. * / 
    BOOL empty () {
         return q1.empty () && q2.empty (); 
    } 
}; 

int main () 
{ 
    MyStack * obj = new new MyStack (); 
    obj -> Push ( . 1 ); 
    obj -> Push ( 2 );
    obj->push(3);
    cout << obj->top() << endl;
    cout <<obj->pop() << endl;
    cout << obj->top() << endl;
    cout <<obj->pop() << endl;
    cout << obj->top() << endl;
}

 

 

Reference Links: https://blog.csdn.net/violet_echo_0908/article/details/50986516

Guess you like

Origin www.cnblogs.com/lfri/p/12436929.html