1. TopicUse two stacks to implement a queue
2. Request
You can only use the pop (), Top () and push () of the stack, and test the stack for empty empty () four operations. To implement the queue's empty (), push (), pop (), Back (), front (), and so on.
3, ready to do this problem before you know the next stack and queue-related knowledge points. Queue is an advanced first-out, FIFO) data type, he has two ports, data elements can only be entered from one mouth, from another. Queues only allow elements to be added from the end of the team, the team head to delete elements, must conform to the FIFO principle, queue and stack Does not have traversal behavior as well. As shown in the following illustration:
Stack is an advanced Out,filo data structure with only one exit, stack only allows adding elements to the top of the stack, removes elements, gets the top element, but does not allow access to elements other than the top, only the top elements of the stack can be used by the outside world, This means that the stack does not have traversal behavior, and there is no iterator.
4. Summary of Ideas
Implementing the queue's Pop method pop-up team header elements: Stack is a port in and out, also does not support random access, so you can not directly from the bottom of the stack to get the first element. To get the first element, you need to copy all the elements in the left stack container to the right stack container. Because of the stack's advanced data structure, the first element of the left container is stored as the last element in the right stack container, so that you don't get the first element in the left stack container? The back method that implements the queue gets the queue's tail element: logic and Idea 1, the only difference is the pop and top
Use two stacks to implement ⼀ queues. cpp//C + +////Created by Liu Longling on 16/1/29. COPYRIGHT©2016 year liulongling.
All rights reserved.
#include <iostream> #include <stack> using namespace std; Template <class t> class Myqueue {Public:bool empty () const {return head.empty () &&tail.emp
Ty ();
void push (T-t) {Head.push (t); ///Remove the header element//Because queue is an advanced first out, and stack is advanced, you need to copy the data in the head into the tail and then from the tail pop heads element void Pop () {if (
This->empty ()) {//throw exception ("queue is null");
while (!head.empty ()) {Tail.push (Head.top ());
Head.pop ();
///Delete header element Tail.pop ();
The team tail stack container element is then copied to the team header stack container while (!tail.empty ()) {Head.push (Tail.top ());
Tail.pop (); } t& Back () {if (This->empty ()) {//Throw exception (' head is NULL ');
return Head.top (); //Returns the first element t& front () {if (This->empty ()) {//throw exception ("Queue is nul
L ");
while (!head.empty ()) {Tail.push (Head.top ());
Head.pop ();
int tmp = Tail.top ();
The team tail stack container element is then copied to the team header stack container while (!tail.empty ()) {Head.push (Tail.top ());
Tail.pop ();
return TMP;
} private:stack<int> head;
Stack<int> tail;
};
int main () {myqueue<int> q;
for (int i=1;i<5;i++) {Q.push (i);
} cout<< "Front:" <<q.front () <<endl;
cout<< "Back:" <<q.back () <<endl;
return 0;
}
Author: Little Donkey, a game man
Dream: World Peace
Original address: http://blog.csdn.net/liulongling This blog does not indicate reproduced articles to the author of the Small Donkey all, welcomed the reprint, but without the consent of the author must retain this paragraph of the statement, and in the article page obvious location to the original connection, otherwise retain the right to pursue legal responsibility.