# 详解c/c++链式堆栈描述进制转换问题示例

typedef struct node
{
int data;
struct node* next;
}Node,*LPNode;
LPNode creatnode(int data)
{
LPNode newnode = (LPNode)malloc(sizeof(Node));
assert(newnode);
newnode->data = data;
newnode->next = NULL;
return newnode;
}
{
{
}
else
{
LPNode newnode = creatnode(data);
while (pmove->next!=NULL)
{
pmove = pmove->next;
}
pmove->next = newnode;
}
}
{
{
return;
}

if (pronode->next == NULL)
{
cout << pronode->data;
free(pronode);
return;
}
while (posnode->next!= NULL)
{
pronode = posnode;
posnode = posnode->next;
}
cout << posnode->data ;
free(posnode);
pronode->next = NULL;
}

## 创建栈结构

typedef struct stack
{
int top;//用来判断是否为空
}Stack,*LPStack;
//描述一个栈的最初始的状态
LPStack creatstack()
{
LPStack stack = (LPStack)malloc(sizeof(Stack));
assert(stack);
stack->top = 0;
return stack;
}

void push(LPStack stack,int data)
{
stack->top++;
}
void pop(LPStack stack)
{
stack->top--;
}

bool empty(LPStack stack)
{
return stack->top == 0;
}

## 代码实现

int main()
{
LPStack stack = creatstack();
int n;
int num;
cin >> num;//表示数
cin >> n;//表示进制
while (num)
{
push(stack,num%n );
num /= n;
}
while (!empty(stack))
{
pop(stack);
}

return 0;
}