数据结构
#include<stdio.h>#include<stdlib.h>
typedef struct SNode
{
char data;
struct SNode* next;
}SNode, *LinkStack;
int InitStack(LinkStack *top)
{
*top = (LinkStack)malloc(sizeof(SNode));
if (*top == NULL)
{
printf("chu sh hua");
return 0;
}
(*top)->next = NULL;
return 1;
}
int StackEmpty(LinkStack top)
{
if (top->next == NULL)
{
return 1;
}
return 0;
}
int Pop(LinkStack top, char *e)
{
SNode *p;
p = top -> next;
top->next = p->next;
*e = p -> data;
free(p);
return 1;
}
int Push(LinkStack top, char e)
{
SNode *p;
p = (SNode *)malloc(sizeof(SNode));
if (!p)
{
printf("chu cyo ");
return 0;
}
p->data = e;
p->next = top->next;
top->next = p;
return 1;
}
void Convert(int num, int d)
{
LinkStack s;
char ch[] = "0123456789ABCDEF";
char tmp;
InitStack(&s);
do
{
Push(s, ch[num%d]);
num = num/d;
}while(num!=0);
while(!StackEmpty(s))
{
Pop(s, &tmp);
printf("%c", tmp);
}
}
int main(void)
{
int num, d;
printf("input num, d\n");
scanf("%d%d", &num, &d);
Convert(num, d);
printf("\n");
return 1;
}
看看我的水平吧