Data structure one stack and queue (2)

Source: Internet
Author: User

1, two stacks to implement a queue, the implementation of the queue at the end of the insertion node, the head of the queue to delete nodes.

If the queue into the stack1, to out of the queue can be divided into 2 cases: A, if the Stack2 is empty, according to the stack advanced after the characteristics should be stack1 the elements pop out, into the stack2. b, if the Stack2 is not empty, it can be explained that Stack2 is the first several operations by Stack1 out of the stack elements, in line with the principle of FIFO first, then the queue operation can directly make the stack top element in the Stack2 stack can be. Such as:

If there are 3 elements in a queue:

The first case: both Stack1 and Stack2 are empty, the queue is listed into the Stack1, to out of queues, the elements in the stack1 are sequentially out of the stack, push to Stack2, pop out Stack2 the top of the stack element is out of the queue.

The second situation: at this time stack2 not empty if the element 4 to enter the queue into the stack1, to out of the queue, at this time Stack2 is not empty, you can directly pop out of the stack top element 2 can be

The code is as follows:

#include <stack>Template<classT>classqueue{ Public: Queue () {}~Queue () {}voidPushtail (Constt&x) {stack1.push (x); }    voidPophead () {if(Stack2.size () <=0)        {             while(Stack1.size () >0) {T&a =Stack1.top ();                Stack1.pop ();            Stack2.push (a); }        }        if(stack2.size () = =0)            return;    Stack2.pop (); }Private: Stack<T>Stack1; Stack<T>Stack2;};

2, two queue implementation of a stack

The code is as follows:

#include <queue>Template<classT>classstack{ Public: Stack () {}~Stack () {}voidPushhead (Constt&x) {queue1.push (x); }    voidPoptail () {assert (Queue1.size ()>0|| Queue2.size () >0)        if(Queue2.size () <=0)        {             while(Queue.size () >0) {T&a =Queue1.front ();                Queue1.pop ();            Queue2.push (a);        } queue2.pop (); }        if(Queue1.size () <=0)        {             while(Queue2.size () >0) {T&a =Queue2.front ();                Queue2.pop ();            Queue1.push (a); }
            Queue1.pop ();
            }


Private :
Queue<T> queue1;
Queue<T>
};

3, define the data structure of the stack, the implementation of a minimum element can find the min function, call min, pop, push the time Complexity of O (1);

Analysis: If each new element into the stack, sorting to let the smallest element at the top of the stack, so that the Min function can achieve the time complexity of O (1), but once changed position can not meet the characteristics of the advanced stack.

If you set a variable in the stack to hold the smallest element in the current stack, you cannot get the smallest element now, once the smallest element pops out.

can set up a secondary stack, each time the data into the stack, the elements into the auxiliary stack can be set to (the new stack element and the minimum value of the previous into the stack element)

The code is as follows: where MinDate is the stack that holds the data, Min is the secondary stack

template<classT>voidPushConstt&x) {
Mindate.push (x); if(min.size () = =0|| X <Min.top ()) {Min.push (x)}ElseMin.push (Min.top ()); }template<classT>voidPopConstt&x) {Assert (Min.size ()>0&& mindata.size () >0) Mindata.pop (); Min.pop ();} Template<classT>T& Min ()Const{assert (Min.size ()>0&& mindata.size () >0) returnMin.pop ();}

Data structure one stack and queue (2)

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.