找错误无法删除链表中第一个值

求各位帮我找找错,删不了第一个元素

img

#include
#include

typedef struct stuNode
{
    char num[50];
    int score;
    struct stuNode *next;

}stu,*pstu;

int main ()
{
    int n,m;
    
    pstu createNode();//创建一个节点 
    pstu createList(int n);//创建链表 其中会调用createList()函数 
    void outputList(pstu head);//输出链表 
    pstu find(pstu head,int m);//查找 
    pstu deleteNode(pstu head,pstu p);//删除 
    printf("请输入要创建链表的个数n:");
    scanf("%d",&n);
    pstu head=createList(n);//创建n个链表 
     printf("原链表的所有值:");
    outputList(head);
    putchar(10);
    printf("请输入要删除的值m:");
    scanf("%d",&m);
    pstu p=find(head,m);
    deleteNode(head,p);
    printf("现在链表的所有值:");
    

    outputList(head);
    return 0;
} 
pstu createNode()
{
    pstu pnew=(pstu)malloc(sizeof(stu));
    printf("请输入学号 成绩:"); 
    scanf("%s%d",pnew->num,&(pnew->score));
    pnew->next=NULL;
    return pnew;  
}

pstu createList(int n)
{
    pstu pend,p,head;
    int i;
    if(n>=1)    //作用: 创建第一个节点 
    {
        p=createNode();//调用createList函数来创建第一个节点 
        head=p;        
        pend=p;//尾节点始终指向最后一个节点 
    }    
    for(i=2;i<=n;i++)
    {
        p=createNode();//反复调用createList()生成节点 
        pend->next=p;
        pend=p;//尾指针pend始终指向最后一个节点 
    }
    if(n>=1)
        return head;
    else
    
        return NULL;
}

void outputList(pstu head)
{
    if(head!=NULL)
    {
        pstu p=head;
        while(p!=NULL)
        {
        
            printf("%4d",p->score);
            p=p->next;    
        }
    }

}
pstu deleteNode(pstu head,pstu p)
{
    pstu q;
    if(p==NULL) return head;            //如果p为空 那就啥也不删结束就行 
    
    if(p==head) head=head->next;    //如果删除的是第一个节点 
    
    else                            //如果不上以上两种情况,那么要删除的节点就是链表中的节点 
        {
            q=head;                    //q指向第一个节点(保护head指针不能动)
             
            while(q->next!=p)        //while作用:让q指向要 删除节点(p)前一个节点 
                q=q->next;     
                
            q->next=p->next;        //q->next=p->next  删除(跨过)p节点        
        }
        free(p);                    //释放p指向的空间 
        return head;    
}
pstu find(pstu head,int m)
{
    pstu p=head;
    while((p!=NULL)&&(p->score!=m))
        p=p->next;
    if(p==NULL)
        return NULL;
    
    return p;
}










没什么问题,在主函数里第30行调用 pstu deleteNode(pstu head, pstu p);删除函数时错误,调用时改为:head = deleteNode(head, p); ,供参考:

#include <stdio.h>
#include <stdlib.h>
typedef struct stuNode
{
    char num[50];
    int score;
    struct stuNode* next;
}stu, * pstu;
int main()
{
    int n, m;
    pstu createNode();//创建一个节点 
    pstu createList(int n);//创建链表 其中会调用createList()函数 
    void outputList(pstu head);//输出链表 
    pstu find(pstu head, int m);//查找 
    pstu deleteNode(pstu head, pstu p);//删除 

    printf("请输入要创建链表的个数n:");
    scanf("%d", &n);
    pstu head = createList(n);//创建n个链表 

    printf("原链表的所有值:");
    outputList(head);

    putchar(10);

    printf("请输入要删除的值m:");
    scanf("%d", &m);
    pstu p = find(head, m);
    head = deleteNode(head, p); //修改 deleteNode(head,p); 

    printf("现在链表的所有值:");
    outputList(head);
    
    return 0;
}
pstu createNode()
{
    pstu pnew = (pstu)malloc(sizeof(stu));
    printf("请输入学号 成绩:");
    scanf("%s%d", pnew->num, &(pnew->score));
    pnew->next = NULL;
    return pnew;
}
pstu createList(int n)
{
    pstu pend, p, head;
    int i;
    if (n >= 1)    //作用: 创建第一个节点 
    {
        p = createNode();//调用createList函数来创建第一个节点 
        head = p;
        pend = p;//尾节点始终指向最后一个节点 
    }
    for (i = 2; i <= n; i++)
    {
        p = createNode();//反复调用createList()生成节点 
        pend->next = p;
        pend = p;//尾指针pend始终指向最后一个节点 
    }
    if (n >= 1)
        return head;
    else
        return NULL;
}
void outputList(pstu head)
{
    if (head != NULL)
    {
        pstu p = head;
        while (p != NULL)
        {
            printf("%4d", p->score);
            p = p->next;
        }
    }
}
pstu deleteNode(pstu head, pstu p)
{
    pstu q;
    if (p == NULL) return head;       //如果p为空 那就啥也不删结束就行 
    if (p == head) head = head->next; //如果删除的是第一个节点 
    else                            //如果不上以上两种情况,那么要删除的节点就是链表中的节点 
    {
        q = head;                  //q指向第一个节点(保护head指针不能动)
        while (q->next != p)       //while作用:让q指向要 删除节点(p)前一个节点 
            q = q->next;
        q->next = p->next;        //q->next=p->next  删除(跨过)p节点        
    }
    free(p);                    //释放p指向的空间 
    return head;
}
pstu find(pstu head, int m)
{
    pstu p = head;
    while ((p != NULL) && (p->score != m))
        p = p->next;
    if (p == NULL)
        return NULL;
    return p;
}