静态数组实现堆栈

 

#include < assert.h >
#include
< stdio.h >

#if  defined(stack_size)
#else  
#define  stack_size 100 
#endif
extern   int  array[stack_size];
extern   int  top_element;

int   is_empty( void )
{
    
return  top_element  ==   - 1 ;
}
int   is_full( void )
{
    
return  top_element  ==  stack_size - 1 ;
}
void   push( int  value)
{
    assert(
! is_full());
    top_element 
+=   1 ;
    array[top_element] 
=  value;    
    printf(
" top_element = %d  top = %d " ,array[top_element],top_element);
}
int    top( void )
{
    assert(
! is_empty());

    
return  array[top_element] ;
}
void   pop( void )
{
    assert(
! is_empty());
    array[top_element] 
=   0 ;
    top_element 
-=   1 ;    
}

 

 

 

//
// 17_2_2.cpp
// static array achieve stack
// by:chlaws
//
#include < stdio.h >
#include
< stdlib.h >
#include
< string .h >
#include
" stack.c "

#define    stack_size  100
int  array[stack_size]  =  { 3 , 2 , 1 , 4 , 2 , 3 , 6 };
int  top_element  =   - 1 ;

/* int   push(int value);
int   top(void);
void  pop(void);
void  is_empty(void);
void  is_full(void);
*/
int  main( void )
{
    
    
int  len  =   - 1 ;
    
while (array[ ++ len]  !=   0 );
    top_element 
+=  len;
    
int  clue_on  =   0 ;
    
do {
        
int  num;
        
int  bois_key;
        
char  notic[]  =  {
            
" 1.>push num. "
            
" 2.>get top_element. "
            
" 3.>pop num. "
            
" 4.>look over stact is empty. "
            
" 5.>look over stact is full. "
        };
        puts(notic);
        fflush(stdin);
        scanf(
" %d " , & bois_key);
        printf(
" bois_key = %d " ,bois_key);
        fflush(stdin);
        
switch (bois_key)
        {
        
case   1 : scanf( " %d " , & num);push(num); break ;
        
case   2 : printf( " top_element = %d " ,top());  break ;
        
case   3 : pop();  break ;
        
case   4 if (is_empty())printf( " stack is empty! " );
            
else  printf( " no empty " );
            
break ;
        
case   5 if (is_full())printf( " stack is full " );
            
else  printf( " no full! " );
            
break ;
        
default  : exit( 1 ); 
        }
            printf(
" input:1>contine   2>end " );
            fflush(stdin);
            scanf(
" %d " , & clue_on);
    }
while (clue_on  ==   1 );  //  regard if you continue to clue_on

    
return    0 ;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值