查寻删除节点

查找删除节点
C/C++ code

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

struct Node
{
    int data;
    struct Node * pNext;
};

struct Node * Greate_list(void);   //创建链表
void Traversal_list(struct Node * pHead);  //遍历链表
void delete_list(struct Node * pHead); //删除节点

int main(void)
{
    struct Node * pHead;  // 头指针

    pHead = Greate_list();    //创建一个链表
    Traversal_list(pHead);   //遍历一个链表
    delete_list(pHead);   //删除节点


    return 0;
}

struct Node * Greate_list(void)  //创建一个链表
{
    int i;
    int len; //链表结点的个数
    int value;  //存放临时结点的数据域

    struct Node * head_node = (struct Node *)malloc(sizeof(struct Node)); //动态建造一个头结点
    struct Node * interim_node;  //创建临时结点, 让其永远都指向尾结点
    if (NULL == head_node)
    {
        printf("内存非配失败!");
        exit (-1);
    }

    interim_node = head_node;  //让临时结点 指向 头结点
    interim_node->pNext = NULL; // 头结点指针域为空(NULL)
    
    printf("请输入要创建结点的个数: ");
    scanf("%d", &len);

    for (i = 0; i < len; i++)
    {
        printf("请输入第一个结点的值: ", i+1);
        scanf("%d", &value);

        struct Node * New = (struct Node *)malloc(sizeof(struct Node));  //动态的建造每一个结点
        if (NULL == New)
        {
            printf("内存非配失败!");
            exit (-1);
        }
        
        New->data = value;
        interim_node->pNext = New;
        New->pNext = NULL;
        interim_node = New;
    }

    return head_node;
}

void Traversal_list(struct Node * pHead)   //遍历输出
{
    struct Node * p;

    if (pHead->pNext == NULL)
    {
        printf("链表为空!\n");
        exit (1);
    }
    p = pHead->pNext;
    while (NULL != p)
    {
        printf("%d  ", p->data);
        p = p->pNext;
    }
    printf("\n");

    return;
}
void delete_list(struct Node * pHead)
{
    int n;
    struct Node * p ,*q;

    p = pHead->pNext;
    printf("输入要删除的元素: ");
    scanf("%d", &n); 

    while (p != NULL)
    {
        if (p->data == n)  
        {
            q = p;
            p = q->pNext;
            free(q);
            Traversal_list(pHead);  
            return;
        }
        else
        {
            p = p->pNext;
        }
    }
    printf("抱歉,找不到你要删除的数字!\n");
    return;
}



运行输入后程序出错,我知道出错在delete_list函数if判断里但不知道怎么改啊。

------解决方案--------------------
C/C++ code

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

struct Node
{
    int data;
    struct Node * pNext;
};

struct Node * Greate_list(void);   //创建链表
void Traversal_list(struct Node * pHead);  //遍历链表
void delete_list(struct Node * pHead); //删除节点

int main(void)
{
    struct Node * pHead;  // 头指针

    pHead = Greate_list();    //创建一个链表
    Traversal_list(pHead);   //遍历一个链表
    delete_list(pHead);   //删除节点


    return 0;
}

struct Node * Greate_list(void)  //创建一个链表
{
    int i;
    int len; //链表结点的个数
    int value;  //存放临时结点的数据域

    struct Node * head_node = (struct Node *)malloc(sizeof(struct Node)); //动态建造一个头结点
    struct Node * interim_node;  //创建临时结点, 让其永远都指向尾结点
    if (NULL == head_node)
    {
        printf("内存非配失败!");
        exit (-1);
    }

    interim_node = head_node;  //让临时结点 指向 头结点
    interim_node->pNext = NULL; // 头结点指针域为空(NULL)
    
    printf("请输入要创建结点的个数: ");
    scanf("%d", &len);

    for (i = 0; i < len; i++)
    {
        printf("请输入第一个结点的值: ", i+1);
        scanf("%d", &value);

        struct Node * New = (struct Node *)malloc(sizeof(struct Node));  //动态的建造每一个结点
        if (NULL == New)
        {
            printf("内存非配失败!");
            exit (-1);
        }
        
        New->data = value;
        interim_node->pNext = New;
        New->pNext = NULL;
        interim_node = New;
    }

    return head_node;
}

void Traversal_list(struct Node * pHead)   //遍历输出
{
    struct Node * p;

    if (pHead->pNext == NULL)
    {
        printf("链表为空!\n");
        exit (1);
    }
    p = pHead->pNext;
    while (NULL != p)
    {
        printf("%d  ", p->data);
        p = p->pNext;
    }
    printf("\n");

    return;
}
void delete_list(struct Node * pHead)
{
    int n;
    struct Node * p ,*q;

    p = pHead->pNext;
    printf("输入要删除的元素: ");
    scanf("%d", &n); 
    q=p;
    while (p != NULL)
    {
        if (p->data == n)  
        {
           q->pNext=p->pNext;
            free(p);
    
            Traversal_list(pHead);  
            return;
        }
        else
        {
        q=p;//可以记住需要删除的那个节点的前一个节点
            p = p->pNext;
        }
    }
    printf("抱歉,找不到你要删除的数字!\n");
    return;
}