Two stacks S1 and S2 are used to simulate a queue.
- Two stacks S1 and S2 are used to simulate a queue. When an element needs to be inserted into the queue, S1 is used to store the input element, that is, S1 performs the stack operation. When it is necessary to get out of the queue, the stack operation is performed on S2. Since the order of taking out elements from the stack is the reverse of the original order, all elements in S1 must be out of the stack and incorporated into S2, and then the out of queue operation can be realized by performing the out of stack operation in S2. Before performing this operation, you must judge whether S2 is empty, otherwise the order will be confused. When stacks S1 and S2 are empty, the queue is empty.
- The summary is as follows:
- Make a response to the stack out operation of S2. If S2 is empty, first send all elements in S1 to S2.
- The stack operation of S1 is queued. If S1 is full, S2 must be empty before all elements in S1 can be inserted into S2.
enter team exercise do { Stack s 1 not full — can with Follow Continued enter Stack ( Namely can with Follow Continued enter team ) Stack s 1 already full { Stack s 2 no by empty — surface show team column full Stack s 2 by empty — take Stack s 1 in of place have element element whole Department Out Stack , and enter Stack reach s 2 in Queue operation \ begin{cases} stack s1 is not full - you can continue to enter the stack (that is, you can continue to queue) \ \ stack s1 is full \ begin{cases} stack s2 is not empty - indicates that the queue is full \ \ stack s2 is empty - all elements in stack s1 are out of the stack and incorporated into stack s2 \ end {cases} \ \ end {cases} Queue operation ⎩⎪⎨⎪⎧ stack s1 is not full - you can continue to enter the stack (that is, you can continue to queue) stack s1 is full {stack s2 is not empty - indicates that the queue is full and stack s2 is empty - all elements in stack s1 are out of the stack and incorporated into stack s2
Out team exercise do { Stack s 2 no by empty — take Stack s 2 in of place have element element whole Department Out Stack Stack s 1 , s 2 all by empty — surface show team column empty Stack s 2 by empty , Stack s 1 no by empty — take Stack s 1 in of place have element element whole Department Out Stack , and enter Stack reach s 2 in Out of queue operation \ begin{cases} stack s2 is not empty - all elements in stack s2 are out of the stack \ \ stacks s1 and s2 are empty - indicates that the queue is empty \ \ stack s2 is empty, stack s1 is not empty - all elements in stack s1 are out of the stack and incorporated into stack s2 \ \ end{cases} Out of queue operation ⎩⎪⎨⎪⎧ stack s2 is not empty - all elements in stack s2 are out of the stack. Stacks s1 and s2 are empty - indicates that the queue is empty. Stack s2 is empty and stack s1 is not empty - all elements in stack s1 are out of the stack and incorporated into stack s2
#include<iostream> using namespace std; #include<malloc.h> #define MaxSize 10 typedef int ElemType; typedef struct Stack { ElemType data[MaxSize]; int top; }; void InitStack(Stack& s) { s.top = -1; } bool StackEmpty(Stack s) { return s.top == -1; } bool StackOverflow(Stack s) { return s.top == MaxSize - 1; } bool Push(Stack& s, ElemType x) { if (s.top == MaxSize - 1) return false; else { s.data[++s.top] = x; return true; } } bool Pop(Stack& s, ElemType& x) { if (s.top == -1) return false; else x = s.data[s.top--]; return true; } bool enQueue(Stack &s1,Stack &s2, ElemType x) //Queue algorithm { if(!StackOverflow(s1)) //Stack s1 is not full { Push(s1, x); //Push return true; } if (StackOverflow(s1) && !StackEmpty(s2)) //Stack s1 is full, stack s2 is not empty { printf("Queue full"); return false; } if (StackOverflow(s1) && StackEmpty(s2)) //Stack s1 is full and stack s2 is empty { while (!StackEmpty(s1)) //All elements in s1 are out of the stack and incorporated into s2 until stack s1 is empty { Pop(s1, x); //s1 element out of stack Push(s2, x); //Stack out elements in s1 stack in s2 } } Push(s1, x); //When stack s1 is not full, continue to stack return true; } void deQueue(Stack &s1,Stack &s2,ElemType &x) //Out of queue algorithm { if (!StackEmpty(s2)) Pop(s2, x); //When stack s2 is not empty, the elements in s2 are out of the stack else if (StackEmpty(s1)) cout << "Queue is empty" << endl; //Stack s2 is empty, stack s1 is also empty, and the queue is empty else //Stack s2 is empty, stack s1 is not empty { while (!StackEmpty(s1)) //All elements in s1 are out of the stack and incorporated into s2 until stack s1 is empty { Pop(s1, x); //s1 element out of stack Push(s2, x); //Stack out elements in s1 stack in s2 } Pop(s2, x); //When stack s2 is not empty, continue to exit the stack } } bool QueueEmpty(Stack s1,Stack s2) //Algorithm for judging empty queue { if (StackEmpty(s1) && StackEmpty(s2)) return true; //When stack s1 and stack s1 are both empty, the queue is empty else return false; } int main() { Stack s1,s2; InitStack(s1); InitStack(s2); int i; for (i = 1; i <= MaxSize; i++) enQueue(s1, s2, i); //When you need to join the queue, put the elements on the stack first ElemType num; for (i = 0; i <= MaxSize-1; i++) { deQueue(s1, s2, num); cout << num << ' '; } return 0; }
Program analysis: