堆栈实现

/*
** Interface for a stack module
*/


#define  STACK_TYPE int /* Type of value on the stack */

/*
** push
** Pushes a new value on the stack.  The argument is the value
** to be pushed.
*/

void  push( STACK_TYPE value );

/*
** pop
** Pops a value off of the stack, discarding it.
*/

void  pop(  void  );

/*
** top
** Returns the topmost value on the stack without changing the
** stack.
*/

STACK_TYPE top( 
void  );

/*
** is_empty
** Returns TRUE if the stack is empty, else FALSE.
*/

int  is_empty(  void  );

/*
** is_full
** Returns TRUE if the stack is full, else FALSE.
*/

int  is_full(  void  );

*****************************************************************************

静态链表,预先指定长度

 

/*
** A stack implemented with a static array.  The array size can
** be adjusted only by changing the #define and recompiling
** the module.
*/

#include 
" stack.h "
#include 
< assert.h >

#define  STACK_SIZE 100 /* Max # of values on the stack */

/*
** The array that holds the values on the stack, and a pointer
** to the topmost value on the stack.
*/

static  STACK_TYPE stack[ STACK_SIZE ];
static   int   top_element  =   - 1 ;

/*
** push
*/

void
push( STACK_TYPE value )
{
 assert( 
!is_full() );
 top_element 
+= 1;
 stack[ top_element ] 
= value;
}


/*
** pop
*/

void
pop( 
void  )
{
 assert( 
!is_empty() );
 top_element 
-= 1;
}


/*
** top
*/

STACK_TYPE top( 
void  )
{
 assert( 
!is_empty() );
 
return stack[ top_element ];
}


/*
** is_empty
*/

int
is_empty( 
void  )
{
 
return top_element == -1;
}


/*
** is_full
*/

int
is_full( 
void  )
{
 
return top_element == STACK_SIZE - 1;
}


*****************************************************

动态链表,新节点加入动态申请空间

 

/*
** A stack implemented with a linked list.  This stack has no size
** limit.
*/

#include 
" stack.h "
#include 
< stdio.h >
#include 
< stdlib.h >
#include 
< malloc.h >
#include 
< assert.h >

#define  FALSE 0

/*
** Define a structure to hold one value.  The link field will
** point to the next value on the stack.
*/

typedef 
struct  STACK_NODE  {
 STACK_TYPE value;
 
struct STACK_NODE *next;
}
 StackNode;

/*
** A pointer to the topmost node on the stack.
*/

static  StackNode  * stack;

/*
** create_stack
*/

void  create_stack( size_t size )
{
}


/*
** destroy_stack
*/

void  destroy_stack(  void  )
{
 
while!is_empty() )
  pop();
}


/*
** push
*/

void  push( STACK_TYPE value )
{
 StackNode 
*new_node;

 new_node 
= malloc( sizeof( StackNode ) );
 assert( new_node 
!= NULL );
 new_node
->value = value;
 new_node
->next = stack;
 stack 
= new_node;
}


/*
** pop
*/

void  pop(  void  )
{
 StackNode 
*first_node;

 assert( 
!is_empty() );
 first_node 
= stack;
 stack 
= first_node->next;
 free( first_node );
}


/*
** top
*/

STACK_TYPE top( 
void  )
{
 assert( 
!is_empty() );
 
return stack->value;
}


/*
** is_empty
*/

int  is_empty(  void  )
{
 
return stack == NULL;
}


/*
** is_full
*/

int  is_full(  void  )
{
 
return FALSE;
}


 

**********************************************************

动态链表,而且带有随节点变化的长度计数

 

/*
** A stack implemented with a dynamically allocated array.
** The array size is given when create is called, which must
** happen before any other stack operations are attempted.
*/
#include 
" stack.h "
#include 
< stdio.h >
#include 
< stdlib.h >
#include 
< malloc.h >
#include 
< assert.h >

/*
** The array that holds the values on the stack, and a pointer
** to the topmost value on the stack.
*/
static  STACK_TYPE  * stack;
static  size_t  stack_size;
static   int   top_element  =   - 1 ;

/*
** create_stack
*/
void  create_stack( size_t size )
{
 assert( stack_size 
==   0  );
 stack_size 
=  size;
 stack 
=  malloc( stack_size  *   sizeof ( STACK_TYPE ) );
 assert( stack 
!=  NULL );
}

/*
** destroy_stack
*/
void  destroy_stack(  void  )
{
 assert( stack_size 
>   0  );
 stack_size 
=   0 ;
 free( stack );
 stack 
=  NULL;
}

/*
** push
*/
void  push( STACK_TYPE value )
{
 assert( 
! is_full() );
 top_element 
+=   1 ;
 stack[ top_element ] 
=  value;
}

/*
** pop
*/
void  pop(  void  )
{
 assert( 
! is_empty() );
 top_element 
-=   1 ;
}

/*
** top
*/
STACK_TYPE top( 
void  )
{
 assert( 
! is_empty() );
 
return  stack[ top_element ];
}

/*
** is_empty
*/
int  is_empty(  void  )
{
 assert( stack_size 
>   0  );
 
return  top_element  ==   - 1 ;
}

/*
** is_full
*/
int  is_full(  void  )
{
 assert( stack_size 
>   0  );
 
return  top_element  ==  stack_size  -   1 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值