C实现Stack,并通过Stack来检查括号是否匹配

/**
* @file GM_Stack.h
* @brief 
* @author Don Hao
* @date 2011-8-22 22:49:37
* @version 
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>[email protected]</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#ifndef _GM_STACK_H
#define _GM_STACK_H

#include <stdlib.h>

#ifdef __cplusplus
extern"C"
{
#endif /**< __cplusplus */

    /** 
    * @brief GM_Stack_Push 
    * 
    * 压栈
    * @param[in] value 
    * @return int -1失败
    */
    int GM_Stack_Push(int value);

    /** 
    * @brief GM_Stack_Pop 
    * 
    * 出栈
    * @param[out] value 
    * @return int  -1失败
    */
    int GM_Stack_Pop( int* value );

    /** 
    * @brief GM_Stack_Get 
    * 
    * 获得栈顶元素
    * @param[out] value 
    * @return int  -1失败
    */
    int GM_Stack_Get(int* value);

#ifdef __cplusplus
}
#endif /**< __cplusplus */

#endif /**< _GM_STACK_H */

/**
* @file GM_Stack.c
* @brief 
* @author Don Hao
* @date 2011-8-22 22:49:35
* @version 
* <pre><b>copyright: </b></pre>
* <pre><b>email: </b>[email protected]</pre>
* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
* <pre><b>All rights reserved.</b></pre>
* <pre><b>modification:</b></pre>
* <pre>Write modifications here.</pre>
*/
#include "GM_Stack.h"
#include <stdio.h>

typedef struct Stack
{
    int value;
    struct Stack* next;
}Stack_Struct;

static Stack_Struct* top = NULL;

int GM_Stack_Push( int value )
{
    Stack_Struct* tmp = (Stack_Struct*)malloc(sizeof(Stack_Struct));
    
    if (NULL == tmp)
    {
        return -1;
    }
    
    tmp->value = value; 
    tmp->next = NULL;
    
    if (NULL != top)
    {
        tmp->next = top;
    }

    top = tmp;

    return 1;
}

int GM_Stack_Pop( int* value )
{
    Stack_Struct* tmp = NULL;

    if ((NULL == top) || (NULL == value))
    {
        return -1;
    }
    else
    {
        *value = top->value;
        tmp = top;
        top = top->next;

        free(tmp);
        tmp = NULL;

        return 1;
    }   
}

int GM_Stack_Get( int* value )
{
    if ((NULL == value) || (NULL == top))
    {
        return -1;
    }
    else
    {
        *value = top->value;
        return 1;
    }
}

void main()
{
    /** @brief 测试栈的基本操作 */
    int i     = 0;
    int value = 0;
    int rt    = -1;
    int match = 1;
    const char* a = "{abc((de)gg(f)((())))}"; //括弧匹配
    const char* b = "{abc((de)ggf)((())))}"; //括弧不匹配

    for (i = 0; i < 10; ++i)
    {
        rt = GM_Stack_Push(i);
        printf("PUSH rt=%d\n", rt);
    }

    for (i = 0; i < 10; ++i)
    {
        rt = GM_Stack_Pop(&value);

        printf("POP rt=%d: value=%d ", rt, value);
    }

    printf("\n");

    rt = GM_Stack_Pop(&value);

    printf("POP rt=%d: value=%d\n", rt, value);

    rt = GM_Stack_Get(&value);
    printf("GET rt=%d: value=%d\n", rt, value);

    rt = GM_Stack_Push(5);
    printf("POP rt=%d\n", rt);

    rt = GM_Stack_Get(&value);
    printf("GET rt=%d: value=%d\n", rt, value);

    GM_Stack_Pop(&value);

    GM_Stack_Pop(&value);
    /** @brief 通过栈来检测括号是否匹配 */
    while (*a)
    {
        if (('{' == *a) || ('(' == *a))
        {
            GM_Stack_Push(*a);
        }
        else if (')' == *a)
        {
            rt = GM_Stack_Pop(&value);
            if ((-1 == rt) || (value != '('))
            {
                match = -1;
                break;
            }
        }
        else if (')' == *a)
        {
            rt = GM_Stack_Pop(&value);
            if ((-1 == rt) || (value != '{'))
            {
                match = -1;
                break;
            }
        }
        else
        {

        }
        ++a;
    }

    if (-1 == match)
    {
        printf("a: NotMatch\n");
    }
    else
    {
        printf("a: Match\n");
    }

    while (*b)
    {
        if (('{' == *b) || ('(' == *b))
        {
            GM_Stack_Push(*a);
        }
        else if ((')' == *b) || ('}' == *b))
        {
            rt = GM_Stack_Pop(&value);
            if ((-1 == rt) || (value != *b))
            {
                match = -1;
                break;
            }
        }
        else
        {

        }
        ++b;
    }

    if (-1 == match)
    {
        printf("b: NotMatch\n");
    }
    else
    {
        printf("b: Match\n");
    }
}

 

你可能感兴趣的:(C实现Stack,并通过Stack来检查括号是否匹配)