The following code uses depth-first search algorithm to generate permutations recursively, I want to know how to change to iteration equivalent without a stack(Pointers can be used). Note that this is very similar to traversing a tree, I try to use the method mentioned in this link, but it is always unsuccessful.
#include <stdio.h>
#define N 3
int A[N];
void dfs(int n, int mask) {
if (n == N) {
for (int j = 0; j < N; ++j) {
printf("%d ", A[j]);
}
puts("");
} else
for (int i = 0; i < N; i++)
if ((mask & (1 << i)) == 0) {
A[n] = i + 1;
dfs(n + 1, mask | (1 << i));
}
}
int main() {
dfs(0, 0);
}