zoukankan      html  css  js  c++  java
  • SDUT-2134_数据结构实验之栈与队列四:括号匹配

    数据结构实验之栈与队列四:括号匹配

    Time Limit: 1000 ms Memory Limit: 65536 KiB

    Problem Description

    给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。

    Input

    输入数据有多组,处理到文件结束。

    Output

    如果匹配就输出“yes”,不匹配输出“no”

    Sample Input

    sin(20+10)
    {[}]
    

    Sample Output

    yes
    no

    Hint

    Source

    ma6174

    括号匹配,栈的经典题目,将左括号入站,然后遇到右括号就询问栈顶是不是与之相匹配的左括号,如果是,出栈,不是,匹配失败。
    注意这个题目是多组输入,而且包含空格,所以需要用 gets读入。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct node
    {
        char data;
        struct node *next;
    }Node;
    
    typedef struct stack
    {
        Node *base,*top;
    }Stack;
    
    Node *newnode()//建立节点
    {
        Node *t;
        t = (Node *)malloc(sizeof(Node));
        t->next = NULL;
        return t;
    };
    
    Stack *Newstack()//建立新栈
    {
        Stack *t;
        t = (Stack *)malloc(sizeof(Stack));
        t->top = newnode();
        t->base = t->top;
        return t;
    }
    
    void push(Stack *t,char x)//入站
    {
        Node *p = newnode();
        p->data = x;
        p->next = t->top->next;
        t->top->next = p;
        t->base = p;
    }
    
    char top(Stack *t)//询问栈顶元素
    {
        return t->top->next->data;
    }
    
    void pop(Stack *t)//出栈
    {
        Node *p;
        p = t->top->next;
        t->top->next = t->top->next->next;
        free(p);
    }
    
    int empty(Stack *t)//询问栈是否为空
    {
        if(t->top->next==NULL)
            return 1;
        return 0;
    }
    
    void del(Stack *t)//清空栈
    {
        while(!empty(t))
            pop(t);
    }
    
    int main()
    {
        char s[55];
        Stack *t;
        int n,i;
        t = Newstack();
        while(gets(s)!=NULL)
        {
            n = strlen(s);
            for(i=0;i<n;i++)
            {
                if(s[i]=='('||s[i]=='{'||s[i]=='[')
                    push(t,s[i]);
                else if(s[i]==')'||s[i]=='}'||s[i]==']')
                {
                    if(empty(t))
                        break;
                    else
                    {
                        if(s[i]==')'&&top(t)!='(')
                            break;
                        else if(s[i]=='}'&&top(t)!='{')
                            break;
                        else if(s[i]==']'&&top(t)!='[')
                            break;
                        else
                            pop(t);
                    }
                }
            }
            if(i!=n)
                printf("no
    ");
            else
            {
                if(!empty(t))//注意最后要判定是否所有的左括号都已经匹配
                    printf("no
    ");
                else
                    printf("yes
    ");
            }
            del(t);
        }
        return 0;
    }
    

    线性表

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct Stack
    {
        char *top,*base;
    }Stack;
    
    Stack newStack()
    {
        Stack t;
        t.top = (char *)malloc(100050*sizeof(char));
        t.base = t.top;
        return t;
    }
    
    void push(Stack *t,char x)
    {
        *(t->top++) = x;
    }
    
    char top(Stack t)
    {
        return *(t.top-1);
    }
    
    void pop(Stack *t)
    {
        t->top--;
    }
    
    int Empty(Stack t)
    {
        if(t.top==t.base)
            return 1;
        return 0;
    }
    
    void del(Stack *t)
    {
        while(!Empty(*t))
            pop(t);
    }
    
    int main()
    {
        char s[55];
        Stack t;
        int n,i;
        t = newStack();
        while(gets(s)!=NULL)
        {
            n = strlen(s);
            del(&t);
            for(i=0;i<n;i++)
            {
                if(s[i]=='('||s[i]=='['||s[i]=='{')
                    push(&t,s[i]);
                else if(s[i]==')')
                {
                    if(Empty(t)||top(t)!='(')
                        break;
                    else
                        pop(&t);
                }
                else if(s[i]==']')
                {
                    if(Empty(t)||top(t)!='[')
                        break;
                    else
                        pop(&t);
                }
                else if(s[i]=='}')
                {
                    if(Empty(t)||top(t)!='{')
                        break;
                    else
                        pop(&t);
                }
            }
            if(i!=n||!Empty(t))
                printf("no
    ");
            else
                printf("yes
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    iOS开发代码规范
    数组去重方法总结
    iOS进阶学习-网络之数据安全
    OC 动态类型,动态绑定,动态加载
    ios运行某些工程时屏幕上下出现黑边的解决办法
    流程控制和数组(关于JAVA基础注意事项)
    高级函数
    源码
    sql s2 学期的学习
    Java 学习笔记
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9747975.html
Copyright © 2011-2022 走看看