出栈序列中的递归问题
下面是关于出栈序列的问提,其中//??????中间的那个递归应用还是不太懂,那位解释一下,感激不尽!#include <iostream>
using namespace std;
const int n = 4;
struct Queue {
int data;
int index;
};
class StackQueue {
private:
int number;
public:
StackQueue();
~StackQueue();
int getNumber();
void all_Queue(int step,int* save,struct Queue* pop,int last);
};
StackQueue :: StackQueue() {
number = 0;
}
StackQueue :: ~StackQueue() {}
void StackQueue :: all_Queue(int step,int* save,struct Queue* pop,int last) {
if(step == n) {
for(int i=0;i < n;i++)
cout << pop[i].data << " ";
cout << endl;
number++;
return;
}
int flag = 0,count = 0;
//?????????????????????????????
for(int j=0;j < n;j++) {
for(int k=0;k < step;k++)
if(j == pop[k].index) flag = 1;
if(flag == 1) {
flag = 0;
continue;
}
if(step == 0) last = j;
int temp_last = last;
for(temp_last--;temp_last > j;temp_last--) {
for(k=0;k < step;k++)
if(temp_last == pop[k].index)
count++;
}
if(count < last-j-1) {
count = 0;
continue;
}
pop[step].data = save[j];
pop[step].index = j;
all_Queue(step+1,save,pop,j);//递归语句
}
//??????????????????????????????????????///
}
int StackQueue ::getNumber() {
return number;
}
int main() {
int save[n] = {1,2,3,4};
struct Queue pop[n];
StackQueue stackQueue;
stackQueue.all_Queue(0,save,pop,-1);
cout << stackQueue.getNumber() << endl;
return 0;
}