- #include <stdio.h>
- #include <stdlib.h>
- struct TNode
- {
- int number;
- TNode* lchild;
- TNode* rchild;
- };
- struct Stack
- {
- TNode** top;
- TNode** base;
- int size;
- };
- void Init(Stack* s)
- {
- s->size = 100;
- s->base = (TNode**)malloc(sizeof(TNode*)*s->size);
- s->top = s->base;
- }
- void Push(Stack* s,TNode* n)
- {
- if(s->top-s->base>=s->size)
- {
- s->size = s->size+10;
- s->base = (TNode**)realloc(s->base,sizeof(TNode*)*s->size);
- }
- *s->top = n;
- s->top = s->top+1;
- }
- void Pop(Stack* s,TNode* &n)
- {
- if(s->top-s->base<=0) return;
- s->top = s->top-1;
- n = *s->top;
- }
- int Empty(Stack* s)
- {
- if (s->top-s->base<=0) return 1;
- else return 0;
- }
- int main()
- {
- Stack *s;
- s = (Stack*)malloc(sizeof(Stack));
- Init(s);
- TNode* tree;
- tree = (TNode*)malloc(sizeof(TNode));
- tree->number = 1;
- tree->lchild = (TNode*)malloc(sizeof(TNode));
- tree->lchild->number = 2;
- tree->lchild->lchild = NULL;
- tree->lchild->rchild = NULL;
- tree->rchild = (TNode*)malloc(sizeof(TNode));
- tree->rchild->number = 3;
- tree->rchild->lchild = (TNode*)malloc(sizeof(TNode));
- tree->rchild->lchild->number = 4;
- tree->rchild->lchild->lchild = NULL;
- tree->rchild->lchild->rchild = NULL;
- tree->rchild->rchild = (TNode*)malloc(sizeof(TNode));
- tree->rchild->rchild->number = 5;
- tree->rchild->rchild->lchild = NULL;
- tree->rchild->rchild->rchild = NULL;
- TNode* node;
- node = tree;
- while(node)
- {
- printf("%d ",node->number);
- Push(s,node);
- node = node->lchild;
- }
- while(Empty(s)!=1)
- {
- Pop(s,node);
- node = node->rchild;
- while(node)
- {
- printf("%d ",node->number);
- Push(s,node);
- node = node->lchild;
- }
- }
- return 0;
- }
转载于:https://blog.51cto.com/zephiruswt/884061