XTU数据结构(C语言版)

表达式中的小括号可能出现不匹配的情况,用户以字符串的方式输入一个表达式,请用栈这种结构对表达式中小括号的匹配情况进行检测,输出检测结果(匹配还是不匹配)。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{    char data;    
    struct node *next;
 }LStackNode,*LinkStack;
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 PushStack(LinkStack top,char e)
 {
     LStackNode *p;
     if((p=(LStackNode*)malloc(sizeof(LStackNode)))==NULL)
         { 
                printf("内存分配失败!\n");       
                 exit(-1);
         }   
     p->data=e;    
     p->next=top->next;   
     top->next=p;   
     return 1;
     }
int PopStack(LinkStack top,char *e)
{
    LStackNode *p;
     p=top->next;    if(!p)    
     {
             printf("栈已空!\n"); 
              return 0;    
     }
     top->next=p->next;
      *e=p->data;    
      free(p);    
      return 1;
}
int GetTop(LinkStack top,char *e)
{
    LStackNode *p;    
    p=top->next;    if(!p)    
    {
            printf("栈已空!\n");        
            return 0;
     }   
      *e=p->data;    
      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(char e,char ch)
{
    if(e=='('&&ch==')')
          return 1;    
    else
          return 0;
 }int main()
 {
     LinkStack S;    
     char *p;    
     char e;    
     char ch[1024];    
     InitStack(&S);    
     gets(ch);    
     p=ch;    
     while(*p)    
     {
             switch(*p)        
             { 
                        case'(':
                                        PushStack(S,*p++);               
                                         break;            
                         case')':
                                         if(StackEmpty(S))                
                                         {
                                                             printf("缺少左括号!\n");                   
                                                              return;                
                                           }               
                                           else               
                                           { 
                                                     GetTop(S,&e);                    
                                                     if(Match(e,*p))               
                                                            PopStack(S,&e);                   
                                                       else                    
                                                       {                
                                                               printf("右左括号不匹配。\n");                        
                                                               return;                   
                                                          }                
                                       }               
                                        default:               
                                         p++;        
                                      }   
              } 
             if(StackEmpty(S)) 
                  printf("括号匹配!\n");    
            else 
                 printf("缺少右括号!\n");
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43717119/article/details/84646618