看到一个题:将一个十进制的数,转换为十六进制输出。要求手动用栈实现。
以下是代码:
#include
#include
struct stack_s
{
int data;
stack_s * next;
};
void init(stack_s *s)
{
s->next = NULL;
}
void push(stack_s s, int e)
{
stack_s *p = (stack_s)malloc(sizeof(stack_s));
p->data = e;
p->next = s->next;
s->next = p;
}
void pop(stack_s *s)
{
stack_s *p;
p = s->next;
s->next = s->next->next;
free(p);
}
int top(stack_s *s)
{
return s->next->data;
}
bool is_empty(stack_s *s)
{
return s->next == NULL;
}
void convert_print(stack_s *s)
{
while (s)
{
int m = top(s);
pop(s);
if (m < 10)
printf("%d", m);
else
printf("%c", 'A' + m - 10);
}
printf("/n");
}
int main()
{
int n;
scanf_s("%d", &n);
stack_s s;
init(&s);
while (n)
{
push(&s, n % 16);
n /= 16;
}
convert_print(&s);
return 0;
}
结果运行结束后显示:程序已终止。
请问是哪里有问题?谢谢!