链式实现的堆栈

/*_############################################################################
  _##
  _##  链式实现的堆栈
  _##  Author: xwlee                        
  _##  Time: 2006.12.31 
  _##  Chang'an University
  _##  Development condition: win2003 Server+VC6.0
  _##
  _##  link_stack.cpp 文件
  _##########################################################################*/
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define FALSE 0

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

// 指向堆栈中第一个结点的指针,初始化为空.
static stacknode *stack;


// create_stack函数
int create_stack( size_t size )
{
    return 1;
}

// destroy_stack函数
int destroy_stack( void )
{
    while( !is_empty() )
        pop();
    return 1;
}

// push函数
void push( STACK_TYPE value )
{
    stacknode  *new_node;

    new_node = ( STACK_NODE * )malloc( sizeof(stacknode) );
    if( new_node == NULL)
    {
        printf("push is false./n");
        exit(0);
    }
    new_node->value = value;
    new_node->next = stack;
    stack = new_node;
}

// pop函数
void pop( void )
{
    stacknode *first_node;
    if( is_empty() ) // 若堆栈已空,条件成立.
    {
        printf("stack already empty./n");
        exit(0);
    }
    first_node = stack;
    stack = first_node->next;
    free( first_node );
}

// top函数
STACK_TYPE top( void )
{
    if( is_empty() ) // 若堆栈已空,条件成立.
    {
        printf("stack already empty./n");
        exit(0);
    }
    return stack->value;
}

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

// is_full函数
int is_full( void )
{
    return FALSE; // 由于链式堆栈不会填满,所以函数始终返回假
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值