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
", rt); } for (i = 0; i < 10; ++i) { rt = GM_Stack_Pop(&value); printf("POP rt=%d: value=%d ", rt, value); } printf("
"); rt = GM_Stack_Pop(&value); printf("POP rt=%d: value=%d
", rt, value); rt = GM_Stack_Get(&value); printf("GET rt=%d: value=%d
", rt, value); rt = GM_Stack_Push(5); printf("POP rt=%d
", rt); rt = GM_Stack_Get(&value); printf("GET rt=%d: value=%d
", 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
"); } else { printf("a: Match
"); } 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
"); } else { printf("b: Match
"); } }

좋은 웹페이지 즐겨찾기