首先 修正上一节 stackPop 代码的错误
E_State stackPop(linkStack * stack,elelmentType * data){ if (stack->top != stack->bottom) { //首先指向第一个元素. lStackNode * next = stack->bottom; <strong><span style="font-size:18px;color:#ff6666;">*data = stack->top->data;</span></strong> //找到栈顶元素的前驱 while (next->next != stack->top) { next = next->next; } free(stack->top); next->next = NULL; stack->top = next; //忘记加了 stack->len--; return E_State_Ok; } else{ return E_State_Error; } }
下面给出10 进制转换其他进制的 例子:
//进制转换 int _tmain(int argc, _TCHAR* argv[]) { linkStack stack; stackInit(&stack); //需要转换的数字 int number; //几进制 int base; printf("请输入一个10进制数: "); scanf("%d",&number); printf("请输入转换进制数: "); scanf("%d",&base); int oldNumber = number; while (number != 0) { stackPush(&stack,number%base); number = number / base; } int pop; printf("%d 转换为 %d进制数 为:",oldNumber,base); while (stackPop(&stack,&pop) != E_State_Error) { printf("%d",pop); } printf("\n"); stackDestory(&stack); return 0; }
时间: 2024-11-10 13:04:16