Sword finger offer--with two stacks to implement the queue

Source: Internet
Author: User

The algorithm of the sword refers to the offer of an offer is recorded.

The topics are described below:

Implementation of a queue with two stacks, the declaration of the queue is as follows, please implement its two functions Appendtail and Deletehead, respectively, completes the queue at the end of the insertion node and delete nodes in the queue head function.

The queues are declared as follows:

Template <typename t>
class cqueue{public
    cqueue () {}
    ~cqueue () {}

    void Appendtail (const t& node);
    T Deletehead ();
    bool Empty () {return
        (stack1.size () = 0 && stack2.size () = 0);
    stack<t> stack1;
    Stack<t> stack2;

The implementation code is as follows:

Template<typename t>
void cqueue<t>::appendtail (const t& node) {
    Stack1.push (node);

Template <typename t>
T cqueue<t>::d eletehead () {
    if (stack2.size () <= 0) {while
        ( Stack1.size () > 0) {
            t& data = Stack1.top ();
            Stack1.pop ();
            Stack2.push (data);
    if (stack2.size () = = 0)
        throw std::exception ("queue is empty");

    T head = Stack2.top ();
    Stack2.pop ();

    return head;

The idea is to use STACK1 to store the inserted data, and when it is necessary to delete the STACK1 elements are pressed into the stack2, at this time the Stack2 stack top element is the first element inserted, And then delete stack2 elements are also inserted in the order of the elements of the queue, that is, the deletion of the first pop-up stack2 elements, if it is empty, then see if the stack1 is empty, if it is empty, the queue is empty, not empty, the first pop-up to Stack2, And then pops the Stack2 element in turn.

A related topic is the implementation of a stack with two queues, the idea is to press the element when the elements are pressed into the non-empty queue, then in the pop-up element, the Non-empty queue elements are pressed into another empty queue, in addition to the team tail element, at this time the team tail element is the final pressure into the stack element, is also the first pop-up element.

The implementation code is as follows:

#ifndef cstack_h_ #define Cstack_h_ #include <queue> using Std::queue;
    Template <typename t> class cstack{Public:cstack () {} ~cstack () {} void Append (const t& data);
    T Remove ();
    bool Empty () {return q1.empty () && q2.empty ();
    } private:queue<t> Q1;
Queue<t> Q2;

}; Template<typename t> void Cstack<t>::append (const t& data) {//Insert preferred Non-empty queue, default insert queue 1 if two queues are empty (Q1
    . empty ()) {Q2.push (data);
    } else{Q1.push (data);
    } template<typename t> T Cstack<t>::remove () {//delete element, all elements of the Non-empty queue are inserted into the empty queue, except the team tail element, T res;
            if (Q1.empty ()) {while (Q2.size () > 1) {t& data = Q2.front ();
            Q2.pop ();
        Q1.push (data);
        res = Q2.front ();
    Q2.pop ();
            else{while (Q1.size () > 1) {t& data = Q1.front ();
            Q1.pop ();
        Q2.push (data);res = Q1.front ();
    Q1.pop ();
return res; } #endif

Complete code example to view my GitHub.

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.