欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

泛型数组堆栈

程序员文章站 2024-03-15 10:52:41
...

  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;
}
相关标签: 数据结构与算法