利用栈实现括号配对

#ifndef _SEQSTACK_
#define _SEQSTACK_
#endif
#include "stdio.h"
#include "math.h"


  typedef struct
  {
      DataType stack [MaxSize];
      
      int top;
      
      int count;
      
      
  } SeqStack;
 
  void StackInitiate (SeqStack *S)
 
  {
      
      S->top =0;
  }
 
  int StackNotEmpty(SeqStack S)
  {
      
      if (S.top<=0)
      {
          
          printf ("\n\nThis Stack is empty !");
          
          return 0;
      }
      
      else  return 1;
  }
 
  int StackPush (SeqStack *S ,DataType x)
  {
      
      
      if (S->top>=MaxSize)
      
      {
          printf ("The stack is full !");
          
          return 0;
      }
      
      else
      
      {
      
        S->stack[S->top]=x;
         
         S->top++;
         
          S->count++;
         
         return 1;
         
           
       }
  }
 
 
  int StackPop(SeqStack *S,DataType *d)
 
  {
      
      if (S->top<=0)
      
      {
          printf ("There is no element to pop !");
          
          return 0;
      }
      
      else
      
      {

        S->top--;    //if stack is empty ,you should make sure that the value of "S->top" is 0
     
         *d=S->stack[S->top];  //this "stack[]" array should be popped begin with "stack[9]" not stack[10],so we should reduce 1 befor we execute the algorithm  in this line
         
        // printf ("%d",S->top);
         
          //S->top--;
          
          S->count--;
         
         return 1;
         
         
      }
  }
 
  int StackTop(SeqStack S,DataType *d)
 
  {
      
      if (S.top<=0)
      {
          
      //printf (" This stack is empty !");
      
        return 0;
   }
   
   else
   {
       
   *d=S.stack[S.top-1];
   
   return 1;
   
   }
      
  }
 
 
 


#include <stdio.h>

#define MaxSize 100

typedef char DataType ;

#include "SeqStack.h"


void main ()

{
    
    char a[MaxSize];
    
    int i,n;
    
    char p,x,s;
    
    SeqStack MyStack;
    
    StackInitiate(&MyStack);
    
    //StackTop(MyStack,&x);    
    
    scanf ("%d",&n);
        
    for (i=0;i<n+2;i++)
    {
        
        scanf ("%c",&a[i]);                        
    
    if (a[i]=='('||a[i]=='<'||a[i]=='[')
    {        
        
        StackPush(&MyStack,a[i]);
    }
    else    if (a[i]=='>'&&(StackTop(MyStack,&x))&&(x=='<'))
    {
                            
            StackPop(&MyStack,&p);                                      
        
    }
    else    if (a[i]=='>'&&(StackTop(MyStack,&x))&&(x!='<'))
    {
              printf ("\n wrong \n");                
        return ;                                  
        
    }
    else    if (a[i]==')'&&(StackTop(MyStack,&x))&&(x=='('))
    {                
        
            StackPop(&MyStack,&p);                
                        
    }
    else    if (a[i]==')'&&(StackTop(MyStack,&x))&&(x!='('))
    {    
                
             printf ("\n wrong \n");
            return ;            
                        
    }
    
        
    else if ((a[i]==']')&&(StackTop(MyStack,&x))&&(x=='['))
    {    
            
            StackPop(&MyStack,&p);    
                                        
    }
    else if ((a[i]==']')&&(StackTop(MyStack,&x))&&(x!='['))
    {    
    
     printf ("\n wrong \n");
            
            return ;    
                                        
    }
        else if (n%2!=0)
    {    
    
     printf ("\n wrong \n");
            
            return ;    
                                        
    }
   }

    if (StackNotEmpty(MyStack))
    {
        
       printf ("\nThis array of character can not make a pair!\n");
       
       StackTop(MyStack,&p);
       
       printf ("%c",p);
       
//    printf ("\n%d\n",MyStack.count);
    
    }
    
    else
    
    printf ("\nThis array of character can make a pair!\n");        
    
}

 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值