在双向链表的开头删除节点时没有得到输出

Not getting output when deleting a node at starting of doubly linked list

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define pf printf
struct node
{
    struct node *prev, *next;
    int data;
};
struct node *head = NULL;
//function to create a doubly linked list
struct node *create(struct node *head)
{
    int num;
    pf("Enter 0 to terminate :\n");
    pf("Data :");
    scanf("%d", &num);
    struct node *new, *ptr;
    while (num != 0)
    {
        new = (struct node *)malloc(sizeof(struct node));
        new->data = num;
        new->next = NULL;
        new->prev = NULL;
        if (head == NULL)
        {
            head = new;
        }
        else
        {
            ptr = head;
            while (ptr->next != NULL)
            {
                ptr = ptr->next;
            }
            new->next = NULL;
            new->prev = ptr;
            ptr->next = new;
        }
        pf("Data :");
        scanf("%d", &num);
    }
    return head;
}
//function to traverse 
struct node *traverse(struct node *head)
{
    struct node *ptr;
    ptr = head;
    while (ptr != NULL)
    {
        pf("%d\n", ptr->data);
        ptr = ptr->next;
    }
    return head;
}
//function to delete the head node 
struct node *delete_head(struct node *head)
{
    struct node *ptr;
    ptr = head;
    head = head->next;
    head->prev = NULL;
    ptr->next = NULL;
    return head;
}
//here i wanted to delete the head node but i am not getting the output but when i change //delete_head(a) to struct node*del=delete_head(a) and then traverse(del) i am getting the output //as correct. i am not able to figure it out.
int main()
{
    struct node *a;
    a = create(head);
    traverse(a);
   delete_head(a);
    traverse(a);

    return 0;
}

对于初学者这个全局变量的声明

struct node *head = NULL;

没有意义。去掉它。并在 main 中写入

struct node *head = NULL;
head = create(head);
traverse(head);
head = delete_head( head );
traverse( head);

函数delete_head产生内存泄漏,因为指针ptr指向的节点没有被删除。此外,当参数 headhead->next 等于 NULL.

时,它可以调用未定义的行为

并且在 main 中,您必须将返回值设置为 head,如上所示

head = delete_head( head );

函数本身可以如下所示

struct node * delete_head(struct node *head)
{
    if ( head != NULL )
    {
        struct node *ptr = head;
        head = head->next;
        if ( head != NULL ) head->prev = NULL;
        free( ptr );
    }

    return head;
}