这段代码哪里出问题了吗,编译没通过


typedef char E;
struct LNode
{
    E element;
    struct LNode* next;
};typedef struct LNode*Node;
void initStack(Node head)
{
    head->next=NULL;
}
int pushStack(Node head,E element)
{
    Node node=malloc(sizeof(struct LNode));
    if(node==NULL)return 0;
    node->next=head->next;
    node->element=element;
    head->next=node;
    return 1;
}
int isEmpty(Node head)
{
    return head->next==NULL;
}
E popStack(Node head)
{
    Node temp=head->next;
    E e=temp->element;
    head->next=head->next->next;
    free(temp);
    return e;
}
int isValid(char*s)
{
    int len=strlen(s);
    if(len%2==1)return 0;
    struct LNode head;
    initStack(&head);

    for(int i=0;ichar c=s[i];
        if(c=='('||c=='['||c=='[')
            pushStack(&head,c);
        else
            {
                if(isEmpty)return 0;
                if(c==')')
                    {
                        if (popStack(&head)!='(')return 0;
                    }
                else if(c==']')
                    {
                        if(popStack(&head)!='[')return 0;

                    }
                else
                    {
                        if(popStack(&head)!='{')return 0;
                    }

            }
            return isEmpty(&head);
    }
}

img

int isValid(char*s)
函数最后面加一个返回值,因为这个函数里面有可能走不到里面的两个return,需要再最后面加一个return

改动处见注释,供参考:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char E;
struct LNode
{
    E element;
    struct LNode* next;
}; 
typedef struct LNode* Node;
void initStack(Node head)
{
    head->next = NULL;
}
int pushStack(Node head, E element)
{
    Node node = (Node)malloc(sizeof(struct LNode));//修改
    //Node node = malloc(sizeof(struct LNode));
    if (node == NULL)  return 0;
    node->element = element;
    node->next = head->next;
    head->next = node;
    return 1;
}
int isEmpty(Node head)
{
    return head->next == NULL;
}
E popStack(Node head)
{
    Node temp = head->next;
    E e = temp->element;
    head->next = head->next->next;
    free(temp);
    return e;
}
int isValid(char* s)
{
    int len = strlen(s);
    if (len % 2 == 1) return 0;
    struct LNode head;
    initStack(&head);
    for (int i = 0; i < len; i++)
    {
        char c = s[i];
        if (c == '(' || c == '[' || c == '[')
            pushStack(&head, c);
        else
        {
            if (isEmpty(&head))  return 0; //修改
            //if (isEmpty)  return 0; 
            if (c == ')')
            {
                if (popStack(&head) != '(')return 0;
            }
            else if (c == ']')
            {
                if (popStack(&head) != '[')return 0;

            }
            else
            {
                if (popStack(&head) != '{')return 0;
            }
        }
    }
    return isEmpty(&head); //修改
}