void Permute(Stack *out, Stack *in, Stack *spur) { Stack *temp_out; Stack *temp_spur; Stack *temp_in; /* the backups. */ if (StackEmpty(out)) { while (!StackEmpty(spur)) SendTrain(in, spur); TraverseStack(in, PrintEntry); /* A mode is detected, */ total++; /* then print out the results.*/ ClearStack(in); /* release the memory. */ #ifdef __TRAVERSE__ printf("\n"); #endif } else { if (1 - frame_depth % 2) { /* To decide how to send trains. */ SendTrain(spur, out); /* If 'frame_depth' is odd, */ } else { /* 'out' to 'spur'. If 'frame_depth */ SendTrain(in, spur); /* is even, 'spur' to 'in'. */ } temp_out = BackUp(out); temp_spur = BackUp(spur); temp_in = BackUp(in); frame_depth++; /* To enter into a new recursion. */ Permute(temp_out, temp_in, temp_spur); frame_depth--; /* Now back to the higher level of recursion. */ if (!StackEmpty(out) && !StackEmpty(spur)) Permute(out, in, spur); } }
int main() { SqStack S; if(InitStack(&S)) { SElemType e; int i; printf("Init Success\n"); if(IsEmpty(S)) { printf("Stack is empty\n"); } for(i = 0; i<10; i++) { Push(&S, i); } GetTop(S, &e); printf("The first element is %d\n",e); printf("Length is %d\n", GetLength(S)); Pop(&S, &e); printf("Pop element is %d\n",e); TraverseStack(S, *visit); if(DestroyStack(&S)) { printf("\nDestroy Stack Success\n"); } } }