• 52 jmu字符串是否对称 (20分)


    #include <stdio.h>
    #include <stdlib.h>
    struct node
    {
        int data;
        struct node *next;
    };
    typedef struct node *stack;
    stack initStack()
    {
        stack s=(stack)malloc(sizeof(struct node));
        s->next=NULL;
        return s;
    }
    int isEmpty(stack s)
    {
        return s->next==NULL;
    }
    void push(stack s,int x)
    {
        stack p=(stack)malloc(sizeof(struct node));
        p->data=x;
        p->next=s->next;
        s->next=p;
    }
    void pop(stack s,int *x)
    {
        stack p=s->next;
        if(isEmpty(s))
        {
            printf("Stack is empty!\n");
            return;
        }
        s->next=p->next;
        *x=p->data;
        free(p);
    }
    int getTop(stack s)
    {
        return s->next->data;//返回栈底的节点的data的值
    }
    int ExpCorrect(char exp[],int n)
    {
        int i;
        stack s=initStack();
        for(i=0; i<n; i++)
        {
            if(exp[i]=='('||exp[i]=='[')
                push(s,exp[i]);
            else if(exp[i]==')'||exp[i]==']')
            {
                int x;
                if(isEmpty(s))  return 0;
                pop(s,&x);
                if(exp[i]==')'&&x!='(')
                    return 0;
                if(exp[i]==']'&&x!='[')
                    return 0;
            }
        }
        if(isEmpty(s)) return 1;
        else return 0;
    }
    int Palindrome(char str[],int n)
    {
        char *p=str;
        stack s=initStack();
        for(; p<str+n/2; p++)
            push(s,*p);
        if(n%2)    p++;
        while(*p!='\0')
        {
            int x;
            pop(s,&x);
            if(x!=*p)
                return 0;
            p++;
        }
        return 1;
    }
    int main()
    {
        int len;
        char str[100];
        gets(str);
        len=strlen(str);
        if(Palindrome(str,len)){
            printf("yes");
        }else{
        printf("no");
        }
    //    while(1)
    //    {
    //        gets(str);
    //        if(Palindrome(str,strlen(str)))
    //            puts("Yes!");
    //        else
    //            puts("No!");
    //    }
        return 0;
    }
  • 相关阅读:
    手动档和自动档
    关于目标:骑行里程破万的感想
    JavaScript基础学习-iterable
    第一个mybatisplus
    MAVEN安装配置
    List和ArrayList的区别
    mysql安装
    Nginx的命令
    Windows Server 2008/2012/2016允许多个用户同时远程桌面
    soapui模拟桩-4 将模拟桩打包成war包
  • 原文地址:https://www.cnblogs.com/csnd/p/16675731.html
Copyright © 2020-2023  润新知