正在加载图片...
for (int i=l: i<=level-1: ++i)printf (") printf("%c\n", T->data) PrintTree(T->firstchild, level+1) PrintTree(T->nextsibling, level) //以下非课程设计要求部分,可作复习用// 非递归实现树的遍历时需用到栈,故此处将顺序栈的定义及实现复制过来,不过栈元素类型需改变为 指向树的结点的指针类型 BiTree //元素类型与顺序栈类型声明 #define STACK INIt size 100 #define STACK INCrement 10 typedef BiTree ElemType;// BiTree等同于 BiNOde*,说明栈中的元素类型为指向树中结点的指针类型 typedef struct f ElemType base ElemType top int stacksize I SqStack ∥/顺序栈基本操作及其实现 Status InitStack(SqStack &S) /初始化一个顺序栈用S带回 S base=(Elem Type *)malloc(STACK INIT SIZE*sizeof(ElemType)) if(!S base)return ERROR S. stacksize=STACK INIT SIZE return OK. Status DestroyStack(SqStack &S) //销毁一个顺序栈 free(S base) S base=NULL S. top=NULL: s. stacksize=0: Status Clear Stack(SqStack &S)I //将一个顺序栈置空 Status Push(SqStack &S, ElemType e)( /向栈顶压入一个新元素 if((S.top-S.base)==S. stacksize){//当前栈满则重新为栈分配空间 S base=(ElemType *realloc(S base, (S stacks ize+STACKINCREMENT)*sizeof(Elem Type)) f(!S base)return ERROR S. top=S base+S. stacksize; S. stacksizetESTACKINCREMENT return OK Status Pop(SqStack &S, ElemType &e)I /栈顶元素出栈并用e带回其值for(int i=1;i<=level-1;++i)printf(" "); printf("%c\n",T->data); PrintTree(T->firstchild,level+1); PrintTree(T->nextsibling,level); } } //以下非课程设计要求部分,可作复习用// //------非递归实现树的遍历时需用到栈,故此处将顺序栈的定义及实现复制过来,不过栈元素类型需改变为 指向树的结点的指针类型BiTree--- //元素类型与顺序栈类型声明 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef BiTree ElemType;//BiTree等同于BiTNode*,说明栈中的元素类型为指向树中结点的指针类型 typedef struct{ ElemType * base; ElemType * top; int stacksize; }SqStack; //顺序栈基本操作及其实现 Status InitStack(SqStack &S){ //初始化一个顺序栈用S带回 S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if(!S.base)return ERROR; S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; } Status DestroyStack(SqStack &S){ //销毁一个顺序栈 free(S.base); S.base=NULL; S.top=NULL; S.stacksize=0; return OK; } Status ClearStack(SqStack &S){ //将一个顺序栈置空 S.top=S.base; return OK; } Status Push(SqStack &S,ElemType e){ //向栈顶压入一个新元素 if((S.top-S.base)==S.stacksize){ //当前栈满则重新为栈分配空间 S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType)); if(!S.base)return ERROR; S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; } Status Pop(SqStack &S,ElemType &e){ //栈顶元素出栈并用e带回其值
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有