Stack structure
reverses the order of elements in a stack
Problem: Suppose that there is a stack {1,2,3,4,5,6},6 is the top of the stack, 1 is the bottom of the stack, now to reverse the elements in this stack.
Idea: The simplest way to do this is to pop the elements of the stack sequentially into an array, then push the array back into the stack, but this requires an O (n) of auxiliary space.
The following is an algorithm that uses only O (1) of auxiliary space, and we know that we can use recursion to simulate the stack's operation. We use functions to recursively pop elements so that on the recursive function stack there are
{6,5,4,3,2,1},1 is the top of the stack, 6 is the bottom of the stack, and then when recursive returns, how to plug the current element to the bottom of the original stack? Here's another recursion!
The C + + code is implemented as follows:
voidAdd2bottom (stack<int> &s,intval) { inttop; if(S.empty ()) {S.push (val); } Else{Top=S.top (); S.pop (); Add2bottom (S, Val); S.push (top); } }voidReverse (stack<int> &s) {inttop; if(!S.empty ()) {Top=S.top (); S.pop (); Reverse (s); Add2bottom (S, top); } }intMain () {intN; intarray[6] = {1,2,3,4,5,6}; Stack<int>s; for(n=0; n<6; n++) S.push (Array[n]); Reverse (s); while(!S.empty ()) {cout<<s.top () <<Endl; S.pop (); } }
push, pop sequence of the stack
implementing queues with stacks
Set 2 stacks for A and b,a as a queue, B to make a team.
Team Full : A full and b is not empty;
team NULL : Both A and b are empty;
Queue :
(1) If A is not full, push the new element into stack A;
(2) If A is full, all the elements in stack A are then popped out and pushed to Stack B, then the elements are put into stack A;
out Team :
(1) If B is empty, then all elements in stack A are popped out and pushed to Stack B, then the element is out of stack B;
(2) If B is not empty, the stack top element of stack b pops out;
C + + code implementation:
BOOLQueue_empty (stack<int> &s1, stack<int> &S2) { returnS1.empty () &&s2.empty ();}voidEnqueue (stack<int> &s1, stack<int> &s2,intval) {S1.push (val);}voidDequeue (stack<int> &s1, stack<int> &s2,int&val) { inttop; if(S2.empty ()) { while(!S1.empty ()) {Top=S1.top (); S1.pop (); S2.push (top); } } if(!S2.empty ()) {Val=S2.top (); S2.pop (); } Else{cout<<"Error:queue is empty"<<Endl; } }intMain () {intN; intarray[6] = {1,2,3,4,5,6}; Stack<int>S1; Stack<int>S2; for(n=0; n<6; n++) enqueue (S1, S2, array[n]); while(!queue_empty (S1, S2)) {dequeue (S1, S2, N); cout<<n<<Endl; }}
Note: This does not take into account the possible full stack space problem.
In fact, you can also use a stack to simulate the structure of the queue, still with the help of recursive stack, each time you insert elements into the stack, plug it to the bottom of the stack, so that the FIFO queue structure.
The code is as follows:
voidEnqueue2 (stack<int> &s,intval) { inttop; if(S.empty ()) {S.push (val); } Else{Top=S.top (); S.pop (); Enqueue2 (S, Val); S.push (top); } }intMain () {intN; intarray[6] = {1,2,3,4,5,6}; Stack<int>s; for(n=0; n<6; n++) enqueue2 (S, Array[n]); while(!S.empty ()) {N=S.top (); S.pop (); cout<<n<<Endl; } }
implementing stacks with Queues
2 teams are listed as a and b,a used as the queue/team, and B as an auxiliary.
Team Full : A full and b is not empty;
team NULL : Both A and b are empty;
into the stack : inserting new elements into queue A;
Out Stack :
(1) In addition to the last element, all elements of queue A are inserted into queue B;
(2) The last element of queue A is out of the team;
(3) Swap the elements of queue B back to queue A;
voidStack_pop (queue<int> &q1, queue<int> &Q2,int&N) { intI, head; while(!Q1.empty ()) {Head=Q1.front (); Q1.pop (); if(Q1.empty ()) {n=Head; } Else{Q2.push (head); } } while(!Q2.empty ()) {Head=Q2.front (); Q1.push (head); Q2.pop (); }}intMain () {intN; intarray[6] = {1,2,3,4,5,6}; Queue<int>Q1, Q2; for(n=0; n<6; n++) Q1.push (Array[n]); while(!Q1.empty ()) {Stack_pop (Q1, Q2, N); cout<<n<<Endl; }}
A classical algorithm for stack structure