3-8 Stack Simulation Queue (25 points)

Assuming that there are two stacks S1 and S2, please use these two stacks to simulate a queue Q.

The so-called simulating queue with the stack is actually by calling the following operation functions of the stack:

int IsFull(Stack S) : judge whether the stack S is full, return 1 or 0;
int IsEmpty (Stack S) : judge whether the stack S is empty, return 1 or 0;
void Push(Stack S, ElementType item) : return the element The item is pushed into the stack S;
ElementType Pop(Stack S) : delete and return the top element of S.
Realize the operation of the queue, that is, enqueue void AddQ (ElementType item) and dequeue ElementType DeleteQ().

Input format: The
input first gives two positive integers N1 and N2, indicating the maximum capacity of the stack S1 and S2. Then a series of queue operations are given: A item represents the entry of the item (here, assuming that item is an integer number); D represents the dequeue operation; T represents the end of the input.

Output format:
For each D operation in the input, output the corresponding dequeued number, or the error message ERROR:Empty. If the enqueue operation cannot be executed, ERROR:Full is also required to be output. Each output occupies 1 line.

Insert picture description here

Insert picture description here

Question ideas:
1. You don't need to really implement the functions of so many functions mentioned in the question, you can use two integer arrays to simulate it.
2. If you want to achieve the first-in-first-out effect of the queue, then you must let the data "turn over" in another stack after being pushed into the stack, that is to say, let the small stack be the input stack and the large stack as the output stack ( Because the small stack is short enough to "turn over" in the large stack).
3. Queue empty condition, both stacks (arrays) are empty.
4. Stack full condition, the input stack is full and there are still elements in the output stack. (Drawing on paper by yourself, under this condition, if you perform a "turn over" action, then the element that was originally in the output stack to be output will be pushed down, which does not comply with the queue rules)

#include<iostream>
using namespace std;
void swap(int& a, int& b){
    
    	int t = a;	a = b;	b = t;}

int main()
{
    
    
	int m, n, x;
	int st1[100], st2[100], k1 = 0, k2 = 0;
	cin >> m >> n;
	if(m < n) swap(m, n);	//把小栈大小定位n,方便统一处理
					
	char c;
	while (cin >> c && c != 'T')
	{
    
    
		if (c == 'A')
		{
    
    
			cin >> x;
			if (k2 == n && k1)	//栈满
				cout << "ERROR:Full\n";
			else if (k2 < n)
				st2[k2++] = x;
			else if (k2 == n && !k1)
			{
    
    
				while (k2)//翻身
				{
    
    
					st1[k1++] = st2[--k2];
				}
				st2[k2++] = x;	//翻身以后再输入
			}
			else
				cout << "ERROR:Full\n";
 
		}
		else if (c == 'D')
		{
    
    
			if (!k1 && !k2)//栈空
				cout << "ERROR:Empty\n";
			else if (k1)
				cout << st1[--k1] << endl;
			else if (!k1 && k2)	
			{
    
    
				while (k2)		//翻身
				{
    
    
					st1[k1++] = st2[--k2];
				}
				cout << st1[--k1] << endl;//翻身以后再输出
			}
		}
	}
    
	return 0;
}

Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/113894078