数据结构实验之栈与队列一:进制转换 发表于 2017-10-15 | 分类于 C++ 今天在acm中提交了无数遍的这个题全都是WA 生气啊 生气啊 生气啊 后来把进制转换的pop换成了traverse就AC了 至今不知道为什么下面是AC的代码: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135#include <stdio.h>#include <stdlib.h>#include <math.h>//建立节点typedef struct node{ int data; struct node *next;}node ,*pnode;//建立栈typedef struct Stack{ pnode top; pnode bottom;}Stack ,*pstack;//栈的初始化void initstack(pstack s){ s->top=(pnode)malloc(sizeof(node)); s->bottom=s->top; s->top->next=NULL; return ;}//压栈void push(pstack s,int e){ pnode p; p=(pnode)malloc(sizeof(node)); p->data=e; p->next=s->top; s->top=p;}//判断栈是否为空int empty(pstack s){ if(s->top==s->bottom) return 1; else return 0;}//出栈bool pop(pstack s,int *e){ if(empty(s)) { return false; } else { pnode p=s->top; *e=p->data; s->top=p->next; free(p); p=NULL; }}void traverse(pstack s){ pnode p=s->top; while(p!=s->bottom) { printf("%d",p->data); p=p->next; } printf("\n");}int main(){ Stack s; initstack(&s); int n,m; int i; scanf("%d %d",&n,&m); while(n>=m) { push(&s,n%m); n=n/m; } push(&s,n); int cou; traverse(&s);} /* while(pop(&s,&cou)) { printf("%d",cou); } printf("\n");} if(n==0) { printf("0\n"); } if(n>0) { for(i=0;n;i++) { push(&s,n%m); n=n/m; } int c=i; int cou; while(pop(&s,&cou)) { printf("%d",cou); } printf("\n"); }}*//*int main(){ Stack s; initstack(&s); int i; for(i=1;i<=9;i++) { push(&s,i); } int cou; while(pop(&s,&cou)) { printf("%d ",cou); }}*/