单链表删除结点,显示段错误,并且也没有输出


struct ListNode *deletem( struct ListNode *L, int m ){
    struct ListNode *p,*q,*r;
    p = (struct ListNode*)malloc(sizeof(struct ListNode));    
    q = (struct ListNode*)malloc(sizeof(struct ListNode));
    if(L->data == m) L = L->next;
    p = L;
    while(p != NULL){
        if(p->data == m){
            p = p->next;
            r = q->next;
            q->next = p;
            free(r);
        }
            q = p;
            p = p->next;
    }
    return L;
}

这是相关函数代码,应该是在这里面出问题了

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}

/* 你的代码将被嵌在这里 */
struct ListNode *readlist(){
    struct ListNode *head, *p,*q;
    int x;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next = NULL;
    q = head;
    while(1){
        scanf("%d",&x);
        if(x == -1) break;
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->next = NULL;
        p->data = x;
        q->next = p;
        q = p;
    }
    return head->next; 
}
struct ListNode *deletem( struct ListNode *L, int m ){
    struct ListNode *p,*q,*r;
    p = (struct ListNode*)malloc(sizeof(struct ListNode));    
    q = (struct ListNode*)malloc(sizeof(struct ListNode));
    if(L->data == m) L = L->next;
    p = L;
    while(p != NULL){
        if(p->data == m){
            p = p->next;
            r = q->next;
            q->next = p;
            free(r);
        }
            q = p;
            p = p->next;
    }
    return L;
}

完整代码
输入样例:
10 11 10 12 10 -1
10
输出样例:
11 12

修改处见注释,供参考:

#include <stdio.h>
#include <stdlib.h>
struct ListNode {
    int    data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p->next) {        //修改
           printf("%d ", p->next->data); //修改
           p = p->next;
     }
     printf("\n");
}

int main()
{
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);
    return 0;
}

/* 你的代码将被嵌在这里 */
struct ListNode *readlist(){
    struct ListNode *head, *p,*q;
    int x;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next = NULL;
    q = head;
    while(1){
        scanf("%d",&x);
        if(x == -1) break;
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->next = NULL;
        p->data = x;
        q->next = p;
        q = p;
    }
    return head; //head->next;  //修改
}
struct ListNode *deletem( struct ListNode *L, int m ){
    struct ListNode *p,*q,*r;
    //p = (struct ListNode*)malloc(sizeof(struct ListNode)); //修改
    //q = (struct ListNode*)malloc(sizeof(struct ListNode)); //修改
    //if(L->data == m) L = L->next;
    p = L;
    while(p->next != NULL){ //while(p != NULL)  //修改
        if(p->next->data == m){
            q = p->next;
            p->next = q->next;
            //q->next = p;    //修改
            free(q);
        }
        else{
            //q = p;         //修改
            p = p->next;
        }
    }
    return L;
}