Chinaunix首页 | 论坛 | 博客
  • 博客访问: 327356
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: Delphi

2012-03-10 21:12:20

#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 ;
}
阅读(1682) | 评论(0) | 转发(0) |
0

上一篇:LinkList

下一篇:LinkStack

给主人留下些什么吧!~~