泛型数组堆栈

  g_stack.h如下:/* * GENERIC implementation of a stack with a static array. The array * size is given as one of the arguments when the stack is instantiated * 用静态数组实现一个泛型的堆栈,数组的长度当堆栈实例化时作文参数给出 */#include <assert.h>#define GENERIC_STACK( STACK_T
摘要由CSDN通过智能技术生成

  g_stack.h如下:

/*
 * GENERIC implementation of a stack with a static array. The array
 * size is given as one of the arguments when the stack is instantiated
 * 用静态数组实现一个泛型的堆栈,数组的长度当堆栈实例化时作文参数给出
 */
#include <assert.h>

#define GENERIC_STACK( STACK_TYPE, SUFFIX, STACK_SIZE ) \
                                                        \
    static STACK_TYPE stack##SUFFIX[ STACK_SIZE ];      \
    static int top_element##SUFFIX = -1;                \
                                                        \
    int                                                 \
    is_empty##SUFFIX( void )                            \
    {                                                   \
        return top_element##SUFFIX == -1;               \
    }                                                   \
                                                        \
    int                                                 \
    is_full##SUFFIX( void )                             \
    {                                                   \
        return top_element##SUFFIX == STACK_SIZE - 1;   \
    }                                                   \
                                                        \
    void                                                \
    push##SUFFIX( STACK_TYPE value )                    \
    {                                                   \
        assert( !is_full##SUFFIX() );                   \
        top_element##SUFFIX += 1;                       \
        stack##SUFFIX[ top_element##SUFFIX ] = value;   \
    }                                                   \
                                                        \
    void                                                \
    pop##SUFFIX( void )                                 \
    {                                                   \
        assert( !is_empty##SUFFIX() );                  \
        top_element##SUFFIX -= 1;                       \
    }                                                   \
                                                        \
    STACK_TYPE top##SUFFIX( void )                      \
    {                                                   \
        assert( !is_empty##SUFFIX() );                  \
        return stack##SUFFIX[ top_element##SUFFIX ];    \
    }

  main.c如下:

/* A client that uses the generic stack module to create two stacks holding
   different types of data. 一个使用泛型堆栈模块创建两个容纳不同类型数据的堆栈的用户数量 */
#include <stdlib.h>
#include <stdio.h>
#include "g_stack.h"

/* Create two stacks, one of integers and one of floats
   创建两个堆栈,一个用于容纳整数,一个用于容纳浮点数 */
GENERIC_STACK ( int, _int, 10 )
GENERIC_STACK ( float, _float, 5 )

int main ( void ) {
    /* Push several values on each stack 往每个堆栈压入几个值 */
    push_int ( 5 );
    push_int ( 22 );
    push_int ( 15 );
    push_float ( 25.3 );
    push_float ( -40.5 );

    /* Empty the integer stack and print the values 清空整数堆栈并打印这些值 */
    while ( !is_empty_int() ) {
        printf ( "Popping %d\n", top_int() );
        pop_int();
    }

    /* Empty the float stack and print the values 清空浮点堆栈并打印这些值 */
    while ( !is_empty_float() ) {
        printf ( "Popping %.1f\n", top_float() );
        pop_float();
    }

    return EXIT_SUCCESS;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值