1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
//建立节点 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("0n"); } 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); } } */
|
近期评论