堆栈实验第二题

来源:互联网 发布:邓禹 知乎 编辑:程序博客网 时间:2024/05/03 00:45
/*设计算法判断一个算术表达式的圆括号是否正确配对*/
 #include"stdio.h"
 #include"malloc.h"
 
 #define maxlen 100
 
 typedef struct node{
     char data[maxlen];
     int top;
 }SeqStack;
 
 
 
  //建立空栈
 SeqStack *SetStack (){
     SeqStack *S;
     S=(SeqStack*)malloc(sizeof(SeqStack));
     S->top=-1;
     return S;
      }
 //顺序栈置空算法
 SeqStack *InintStack(SeqStack *S){
     S->top = -1;
     return S;
 }

//判断栈空算法
int StackEmpty(SeqStack *S){
    if(S->top>=0)
        return 0;
    else
    return 1;
}

//判断栈满算法

int StackFull (SeqStack *S){
    if(S->top<maxlen-1 && S->top>=0)
        return 0;
    else
    return 1;
}

//取栈顶元素

char GetTop(SeqStack *S){
    if(S->top<=maxlen-1 && S->top>=0)
    return (S->data[S->top]);
    else
    printf("error");
}

//入栈
void Push (SeqStack *S,char x) {
    if(S->top<maxlen-1 && S->top>=-1){
        S->top++;
        S->data[S->top]=x;
    }
    else
    printf("error");
}

//出栈

void Pop(SeqStack *S){
    if(S->top>=0)
        S->top--;
    else
    printf("error");
}
    
 int main(){
     int i;
      SeqStack *S;
      S=SetStack();
      char a[maxlen]={'(',')','(','+','-','-','(','8',')'};
      for(i=0;i<maxlen;i++){
          if(a[i]=='('){
              Push(S,a[i]);
          }
          else if(a[i]==')'){
              Pop(S);
          }
      }
      if(StackEmpty(S)){
          printf("配对!");
      }
      else
      printf("不配对!");
 }
0 0
原创粉丝点击