int main() { BiTree T; SqStack S; Initstack(&S); CreateBiTree(&T); // printf("ÏÈÐòµÝ¹é±éÀú"); // PreOrder(T); // printf("\n"); // printf("ÖÐÐòµÝ¹é±éÀú"); // InOrder(T); // printf("\n"); // printf("ºóÐòµÝ¹é±éÀú"); // PostOrder(T); printf("\n"); printf("ÏÈÐò·ÇµÝ¹é±éÀú"); PreOrderUn(T); printf("\n"); // printf("ÖÐÐò·ÇµÝ¹é±éÀú"); // InOrderUn(T); printf("\n"); // printf("ºóÐò·ÇµÝ¹é±éÀú"); // PostOrderUn(&S,T); printf("\n"); return 0; }
int main() { Sqstack S_1, S_2; int i = 0, j = 0; char str[MAXSIZE]; while (gets_s(str) != NULL) { Initstack(&S_1); Initstack(&S_2); for (i = 0; str[i] != NULL; i++) { switch (str[i]) { case'+': { Push(&S_2, str[i]); }break; case'-': { Push(&S_2, str[i]); }break; case'*': { Push(&S_2, str[i]); }break; case'/': { Push(&S_2, str[i]); }break; case'(': { Push(&S_2, str[i]); }break; case')': { Push(&S_2, str[i]); }break; default: { Push(&S_1, str[i]-48); }break; } } PrintStack(&S_1); } return OK; }
int main(){ Stack S; ElemType e; Initstack(&S); printf("enter number!\n"); scanf("%d",e); while(e){ Push(&S,e%8); e=e/8; } while(!Isempty(S)){ Pop(&S,e); printf("%d\n",e); } return 0; }
void InOrder_Non_Recursion(BiTree *bt) { Stack *S; BiTree *p; S = Initstack(); p = bt; while (p || !EmptyStack(S)) { while (p) { push(S, p); p = p->lchild; } pop(S, &p); printf("%c", p->data); p = p->rchild; } }
//非递归先序遍历 void peorder(csTree root) { stackStor s; csTree p; Initstack(&s);//初始化时如果是void 类型则要用二级指针 p=root; while(p!=NULL || IsEmpty(s)!=0) { if(p!=NULL) { printf("%c",p->data); pushStack(s,p); p=p->Firstchild; } else { //中序非递归 printf("%c",p->data); p=popstack(s); p=p->Nextsibling; } } }
void main( ) { char express[30], num[10], theta, tp , d;//express[30]用来读入一个表达式 double a ,b , result, tps; //num[10]用来存放表达式中连接着的数字字符 int t, i, j; int position = 0;//表达式读到的当前字符 Initstack(OPND); Initstack(OPTR); Push(OPTR , '='); printf("input 'b' to run the calc:\n"); scanf("%c" , &d); getchar(); while(d == 'b') { printf( "请输入表达式( 可使用+、-、*、/、(、)、= ): " ) ; gets(express); while(express[position] != '='||Gettop(OPTR , tp) != '=' ) { if(!Is_op(express[position])) { //用两个计数器t和j实现浮点数的读取 t = position;j=0; while(!Is_op(express[position])) { position++; } for(i = t; i<position ; i++ ) {//把表达式中第t到position-1个字符赋给num[10] num[j] = express[i]; j++; } Push(OPND , atof(num)); memset(num,0,sizeof(num));//将num[10]清空 } else { switch(Precede(isp(Gettop(OPTR , tp)),icp(express[position]))) { case '<': Push(OPTR,express[position]);position++;break; case '=': Pop(OPTR , tp) ;position++;break; case '>': { Pop(OPTR , theta) ; Pop(OPND , b) ; Pop(OPND , a) ; result = Operate(a, theta ,b); Push(OPND , result);break; }//end sase }//end switch }//end else }//end while printf("%s%8.4f\n",express,Pop(OPND,tps)); memset(express , 0 , sizeof(express)); position = 0; printf("input 'b' to run the calc again:\n"); scanf("%c" , &d); getchar(); }//end while }
//ÏÈÐò·ÇµÝ¹é±éÀú void PreOrderUn(BiTree T)//SqStack *s, { SqStack s; Initstack(&s); while(T!=NULL || !stackempty(s)) { while(T!=NULL) { push(&s,T); printf("%2c",T->data); T=T->lchild; } if (!stackempty(s)) { T=pop(&s,T); T=T->rchild; } } // push(&s,T); // while(T!=NULL || !stackempty(s)) // { // printf("%2c",T->data); // T=push(&s,T); // if(T->lchild) // push(&s,T); // if(T->rchild) // push(&s,T); // } // push(s,T); // BiTree Q=T; // while(!stackempty(s)) // { // pop(s,T); // if(Q!=NULL) // { // printf("%c",Q->data); // push(s,Q->rchild); // push(s,Q->lchild); // } // } // while(T) // { // while(T) // { // push(s,T); // printf("%2c",T->data); // T=T->lchild; // } // T=pop(s,T); // T=T->rchild; // } // else // T=T->rchild; // T=pop(&s,T); // T=T->rchild; }