#include
#include
typedef int datatype ;
//注意:链栈不含头结点
typedef struct snode
{
datatype data ;
struct snode * next ;
}StackNode , *LinkStack ;
//判断栈空
int isEmpty_LinkStack(LinkStack top)
{
if(top == NULL) return 1 ;
else return 0 ;
}
LinkStack Push_LinkStack(LinkStack top , datatype x)
{
StackNode * s ;
s = (StackNode *)malloc(sizeof(StackNode)) ;
s->data = x ;
s->next = top ;
top = s ;
return top ;
}
LinkStack Pop_LinkStack(LinkStack top ,datatype *x)
{
StackNode * p ;
*x = top->data ;
p = top ; //忘了写这句
top = top->next ;
free(p) ;
return top ;
}
//十进制转换为二进制
LinkStack conversion(LinkStack top , int N , int r)//或者void conversion(LinkStack &top , int N , int r)
{
int x ;
while(N != 0)
{
x = N % r ;
top = Push_LinkStack(top,x) ;
N = N/r ;
}
return top ;
}
int main()
{
LinkStack top ;
datatype x ;
top = (StackNode *)malloc(sizeof(StackNode)) ;
top = NULL ;
scanf("%d",&x) ;
while(x != -999)
{
top = Push_LinkStack(top,x) ;
scanf("%d",&x) ;
}
printf("输出栈中元素:\n") ;
while(top)
{
printf("%d\n",top->data) ;
top = top->next ;
}
printf("十进制转换为二进制") ;
// top = NULL ;
top = conversion(top,8,2) ;
printf("输出栈中元素:\n") ;
while(top)
{
printf("%d\n",top->data) ;
top = top->next ;
}
return 1 ;
}
阅读(1202) | 评论(0) | 转发(0) |