Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2224836
  • 博文数量: 668
  • 博客积分: 10016
  • 博客等级: 上将
  • 技术积分: 8588
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 19:22
文章分类

全部博文(668)

文章存档

2011年(1)

2010年(2)

2009年(273)

2008年(392)

分类:

2009-05-13 11:53:10

#include
#include
#include
#define  STACK_INIT_SIZE  100                // 存储空间的初始分配量
#define  STACKINCREMENT    10                //存储空间分配增量
 
struct SqStack
{
       int *base;
       int *top;       
       int stacksize;                        //当前已分配的存储空间 
};
/*构造一个空栈*/
void InitStack(struct SqStack *S)
{
     S->base = (int *)malloc(STACK_INIT_SIZE * sizeof (int));
     if(!(S->base))                           //存储分配失败 
     {
           exit(0);
           printf("内存分配错误!\n");      
     }
     S->top = S->base ;
     S->stacksize = STACKINCREMENT ;

/*入栈*/
void Push(struct SqStack *S, int e)
{
     if(S->top - S->base >= S->stacksize)      //栈满溢出
     {
           S->base = (int *)realloc(S->base,(STACKINCREMENT+STACK_INIT_SIZE)* sizeof (int));        
                                              //重新分配存储空间
          if(!(S->base))
           {
                exit(0);
                printf("存储失败\n");      
           }
            S->top = S->base + S->stacksize ;
            S->stacksize = S->stacksize + STACKINCREMENT ;
     } 
     * (S->top++)  = e;
}
/*出栈*/
int Pop(struct SqStack *S, int e)
{
     if(S->top == S->base)    //空栈 
          printf("此栈为空栈\n"); 
     e = *( -- S->top);
     return(e);

/*判断栈是否为空*/
int StackEmpty(struct SqStack *S)
{
     if(S->top == S->base)
         return 1;
     else 
         return 0;

/*主函数*/
int main(int argc, char *argv[])
{
  int n = 0;
  int e = 1; 
  struct SqStack *Change;
  Change = (struct SqStack *)malloc(sizeof(sturuct SqStack)); 
  InitStack(Change);
  printf("Please input the Primitive num (十进制数):"); 
  scanf("%d",&n);
  while(n)
  {
       Push(Change , n%8 );
       n = n/8;   
  } 
  printf("The   result   is ( 八 进 制 数 ) : ");
  while(!StackEmpty(Change))
  {                            
         e = Pop(Change,e);
         printf("%d",e); 
                           
  } 
  printf("\n");
  if(e/8 == 0 )
      printf("此栈已经为空了~~~\n"); 
  system("PAUSE"); 
  return 0;
}
阅读(1240) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~