您的位置:首页 > 其它

菜鸟用链栈写的进制转换

2012-02-22 23:35 218 查看
#include<stdio.h>
#include<stdlib.h>

struct Lnode/*以为当时做的题目没有告诉我是多少位的数字所以我选择了长度不受限的链栈*/
{
int num;
struct Lnode *next;
};

int Push(struct Lnode *top , int x)//压入数据
{
struct Lnode *p = (struct Lnode*)malloc(sizeof(struct Lnode));
p->num = x ;
p->next = top->next;//其实压入无非是链表的逆序输入,此为让新的节点指针域指向上一个节点
top->next = p;
return 0;
}

int Pop(struct Lnode *top , int *x )//*x是为了得到弹出的数据
{
struct Lnode *q;
q = top->next;
if( top->next != NULL )//栈不为空的时候
{
*x = q->num;
top->next = q->next;
free(q);/*如果不释放q完全可以让p->next指向下一个,要注意的是这一步不可以跟上不颠倒,q毕竟只是指针,有指向的空间才有意义*/
return 1;
}
else
return 0;
}

void convey(struct Lnode *top,int a ,int b )
{
int t,i,*x,c;

x=&c;
while( a != 0 )
{
t = a % b;
Push(top,t);
a = a/b;
}

i=1;
while(i)
{
i=Pop(top,x);
if(i)
printf("%d",*x);
}
}

void main()
{
int a , b;
struct Lnode *top;
top = (struct Lnode *) malloc (sizeof(struct Lnode));
top->next = NULL;
scanf("%d%d", &a , &b);
convey( top , a ,b );
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: