#include
#include
typedef int datatype ;
#define Max 20
typedef struct
{
datatype data[Max] ;
int top ;
}SeqStack , *StackList ;
SeqStack * Init_SeqStack()
{
SeqStack *s ;
s = (SeqStack *)malloc(sizeof(SeqStack)) ;
s->top = -1 ;
return s ;
}
void Push_SeqStack(SeqStack *s,datatype x)
{
s->top ++ ;
s->data[s->top] = x ;
}
void Pop_SeqStack(SeqStack *s,datatype *x)
{
*x = s->data[s->top] ;
s->top -- ;
}
datatype GetTop_SeqStack(SeqStack * s)
{
return s->data[s->top] ;
}
//十进制转换为二进制
void conversion(SeqStack *s ,int N )
{
int r ;
while(N != 0)
{
r = N%2 ;
Push_SeqStack(s,r) ;
N = N/2 ;
}
}
int main()
{
SeqStack *s ;
s = Init_SeqStack() ;
datatype x ;
int i = 0 ;
scanf("%d",&x) ;
while(x != 0)
{
Push_SeqStack(s,x) ;
scanf("%d",&x) ;
}
printf("取栈顶元素") ;
printf("%d\n",GetTop_SeqStack(s)) ;
//输出
printf("输出栈中元素\n") ;
while(s->top != -1)
{
Pop_SeqStack(s,&x) ;//
printf("%d\n",x) ;
i ++ ;
}
s->top = s->top + i ; //指针恢复到栈顶top
printf("测试栈顶元素恢复情况:") ;
printf("%d\n",GetTop_SeqStack(s)) ;
printf("十进制转换为二进制:8转换为2进制\n") ;
conversion(s,8) ;
//输出
printf("输出栈中元素\n") ;
while(s->top != -1)
{
Pop_SeqStack(s,&x) ;//
printf("%d\n",x) ;
i ++ ;
}
return 1 ;
}
阅读(1715) | 评论(0) | 转发(0) |