1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.  
  4. struct TNode  
  5. {  
  6.     int number;  
  7.     TNode* lchild;  
  8.     TNode* rchild;  
  9. };  
  10.  
  11. struct Stack  
  12. {  
  13.     TNode** top;  
  14.     TNode** base;  
  15.     int size;  
  16. };  
  17.  
  18. void Init(Stack* s)  
  19. {  
  20.     s->size = 100;  
  21.     s->base = (TNode**)malloc(sizeof(TNode*)*s->size);  
  22.     s->top = s->base;  
  23. }  
  24.  
  25. void Push(Stack* s,TNode* n)  
  26. {  
  27.     if(s->top-s->base>=s->size)  
  28.     {  
  29.         s->size = s->size+10;  
  30.         s->base = (TNode**)realloc(s->base,sizeof(TNode*)*s->size);       
  31.     }  
  32.     *s->top = n;  
  33.     s->top = s->top+1;  
  34. }  
  35.  
  36. void Pop(Stack* s,TNode* &n)  
  37. {  
  38.     if(s->top-s->base<=0) return;  
  39.     s->top = s->top-1;  
  40.     n = *s->top;  
  41. }  
  42.  
  43. int Empty(Stack* s)  
  44. {  
  45.     if (s->top-s->base<=0) return 1;  
  46.     else return 0;  
  47. }  
  48.  
  49. int main()  
  50. {  
  51.     Stack *s;  
  52.     s = (Stack*)malloc(sizeof(Stack));  
  53.     Init(s);  
  54.     TNode* tree;  
  55.     tree = (TNode*)malloc(sizeof(TNode));  
  56.     tree->number = 1;  
  57.     tree->lchild = (TNode*)malloc(sizeof(TNode));  
  58.     tree->lchild->number = 2;  
  59.     tree->lchild->lchild = NULL;  
  60.     tree->lchild->rchild = NULL;  
  61.     tree->rchild = (TNode*)malloc(sizeof(TNode));  
  62.     tree->rchild->number = 3;  
  63.     tree->rchild->lchild = (TNode*)malloc(sizeof(TNode));  
  64.     tree->rchild->lchild->number = 4;  
  65.     tree->rchild->lchild->lchild = NULL;  
  66.     tree->rchild->lchild->rchild = NULL;  
  67.     tree->rchild->rchild = (TNode*)malloc(sizeof(TNode));  
  68.     tree->rchild->rchild->number = 5;  
  69.     tree->rchild->rchild->lchild = NULL;  
  70.     tree->rchild->rchild->rchild = NULL;  
  71.     TNode* node;  
  72.     node = tree;  
  73.     while(node)  
  74.     {  
  75.         printf("%d ",node->number);  
  76.         Push(s,node);         
  77.         node = node->lchild;  
  78.     }  
  79.     while(Empty(s)!=1)  
  80.     {  
  81.         Pop(s,node);  
  82.         node = node->rchild;  
  83.         while(node)  
  84.         {  
  85.             printf("%d ",node->number);  
  86.             Push(s,node);         
  87.             node = node->lchild;  
  88.         }  
  89.     }  
  90.     return 0;