1. 程式人生 > >利用棧的基本操作,檢查括號是否匹配

利用棧的基本操作,檢查括號是否匹配

#include <stdio.h>  
	#include <stdlib.h>  
	#include <malloc.h>  
	#include <string.h>  
	typedef char ElemType;  
	typedef struct node  
	{  
	    ElemType data;  
	    struct node *next;  
	}LStackNode,*LinkStack;  
	  
	void InitStack(LinkStack *top);//將鏈棧初始化  
	int StackEmpty(LinkStack top);//判斷鏈棧是否為空  
	int GetTop(LinkStack top,ElemType *e);//取棧頂元素  
	int PushStack(LinkStack top,ElemType e);//進棧操作  
	int PopStack(LinkStack top,ElemType *e);//出棧操作  
	int StackLength(LinkStack top);//求表長操作  
	void DestroyStack(LinkStack top);//銷燬連結串列  
	int Match(ElemType e,ElemType ch);//判斷括號 



#include "括號配對.h"  
	  
	void InitStack(LinkStack *top)//將鏈棧初始化  
	{  
	    if((*top = (LinkStack)malloc(sizeof(LStackNode)))== NULL)  
	    {  
	        exit(-1);  
	    }  
	    (*top)->next = NULL;  
	}  
	  
	int StackEmpty(LinkStack top)//判斷鏈棧是否為空  
	{  
	    if(top->next == NULL)  
	    {  
	        return 1;  
	    }  
        else  
	    {  
	        return 0;  
	    }  
	}  
	  
	int GetTop(LinkStack top,ElemType *e)//取棧頂元素  
	{  
	    LStackNode *p;  
	    p = top->next ;  
	    if(!p)  
	    {  
	        printf("棧已空!");  
	        return 0;  
	    }  
	    *e = p->data ;  
	    return 1;  
	}  
	  
	int PushStack(LinkStack top,ElemType e)//進棧操作  
	{  
	    LStackNode *p;  
	    if((p = (LinkStack)malloc(sizeof(LStackNode))) == NULL)  
	    {  
	        printf("記憶體分配失敗!");  
	        return 0;  
	    }  
	    p->data = e;  
	    p->next = top->next;  
	    top->next = p;  
	    return 1;  
	}  
	  
	int PopStack(LinkStack top,ElemType *e)//出棧操作  
	{  
	    LStackNode *p;  
	    p = top->next ;  
	    if(!p)  
	    {  
	        printf("棧已空!");  
	        return 0;  
	    }  
	    top->next = p->next ;  
	    *e = p->data ;  
	    free(p);  
	    return 1;  
	}  
	  
	int StackLength(LinkStack top)//求表長操作   
	{  
	    LStackNode *p;  
	    int count = 0;  
	    p = top;  
	    while(p->next != NULL)  
	    {  
	        p = p->next ;  
	        count++;  
	    }  
	    return count;  
	}  
	
	void DestroyStack(LinkStack top)//銷燬連結串列  
	{  
	    LStackNode *p,*q;  
	    p = top;  
	    while(!p)  
	    {  
            q = p;  
	        p = p->next ;  
	        free(q);  
	    }  
	}  
	  
	int Match(ElemType e,ElemType ch)  
	{  
	    if(e == '('&&ch == ')')  
	    {  
	        return 1;  
	    }  
	    else if(e == '['&&ch == ']')  
	    {  
	        return 1;  
	    }  
 	    else if(e == '{'&&ch == '}')  
	    {  
	        return 1;  
		}  
	    else  
	    {  
	        return 0;  
		}  
	}  




#include "括號配對.h"  
	int main(void)  
	{  
	    LinkStack S;  
	    char *p;  
	    ElemType e;  
	    ElemType ch[60];  
	    InitStack(&S);  
	    printf("請輸入帶括號的表示式('()','[]','{}').\n");  
	    gets(ch);  
	    p = ch;  
	    while(*p)  
	    {  
	        switch(*p)  
	        {  
	        case '(':  
	        case '[':  
	        case '{':  
	            PushStack(S,*p++);  
	            break;  
	        case ')':  
	        case ']':  
	        case '}':  
	            if(StackEmpty(S))  
	            {  
	                printf("缺少左括號.\n");  
	                return 0;  
	            }  
	            else  
	            {  
	                GetTop(S,&e);  
	                if(Match(e,*p))  
	                {  
	                    PopStack(S,&e);  
	                }  
	                else  
	                {  
	                    printf("左右括號不匹配!\n");  
	                    return 0;  
	                }     
	            }  
	        default:  
	            p++;  
	        }  
	    }  
	    if(StackEmpty(S))  
	    {  
	        printf("括號匹配!\n");  
	    }  
	    else  
	    {  
	        printf("缺少右括號!\n");  
	    }  
	    return 0;  
	}