双向链表删除某一节点

这是一个双向链表,采用尾插法插入数据
问题:为什么第一个节点不能删除,还成了随机数,其他的节点可以正常删除

img

img


代码如下:


#include "stdio.h"
#include "stdlib.h"
typedef struct Node
{
    struct Node *front;
    int data;
    struct Node *next;
}Node;
Node *head=NULL;
void insert(int x) //插入节点,尾插法
{
    Node *temp=(Node *)malloc(sizeof(Node));
    temp->data=x;
    temp->next=NULL;
    if(head==NULL)
    {
        head=temp;
        return 0;
    }
    else
    {
        Node *temp2=head;
        while(temp2->next)
        {
            temp2=temp2->next;
        }
        temp2->next=temp;
        temp->front=temp2;
    }
}
void Print() //输出链表
{
    Node *temp=head;
    while(temp)
    {
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
void dele(int x) //删除链表
{
    Node *temp=head;
    while(temp)
    {
        if(temp->data==x) //判断是否找到了要删除的数据
         { 
            if(temp==head) //判断数据是否为第一个
            head=head->next;
            else
            temp->front->next=temp->next;
            return 0;
         }
        temp=temp->next;
    
}
int main()
{
    int x;
    printf("请输入数据,当输入负数时结束:");
    while(1)
    {
        scanf("%d",&x);
        if(x<0)
        break;
        insert(x);
    }
    printf("数据输入完成:");
    Print();
    printf("请输入要删除的数据:");
    scanf("%d",&x);
    dele(x);
    printf("数据已删除:");
    Print();

}
if(temp==head) //判断数据是否为第一个
{
            if(head->next != NULL)
                  head->next->front = NULL;
            head=head->next;
}

2023.1.23,重新修改如下,见注释,供参考:

#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    struct Node* front;
    int data;
    struct Node* next;
}Node;
Node* head = NULL; 
void insert(int x) //插入节点,尾插法
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->data = x;
    temp->next = NULL;
    temp->front = NULL; 
    if (head == NULL)
        head = temp;
    else
    {
        Node* temp2 = head;
        while (temp2->next)
        {
            temp2 = temp2->next;
        }
        temp2->next = temp;
        temp->front = temp2;
    }
}
void Print() //输出链表
{
    Node* temp = head, * tmp = NULL;
    while (temp)        
    {
        printf("%d ", temp->data);
        tmp = temp;
        temp = temp->next;
    }
    printf("\n");
    while (tmp)        
    {
        printf("%d ", tmp->data);
        tmp = tmp->front;    
    }
    printf("\n");
}
void dele(int x) //删除链表
{
    Node* temp = head;
    while (temp)
    {
        if (temp->data == x) //判断是否找到了要删除的数据
        {
            if (temp == head) { //判断数据是否为第一个  
                head = temp->next; 
                if (temp->next)
                    temp->next->front = NULL; 
            }
            else if (!temp->next) { //链表末尾最后一个结点
                temp->front->next = NULL; 
            }
            else {        //中间结点的处理   
                temp->front->next = temp->next; 
                temp->next->front = temp->front;
            }
            free(temp); 
            return;   
        }
        temp = temp->next;
    }
}
int main()
{
    int x;
    printf("请输入数据,当输入负数时结束:");
    while (1)
    {
        scanf("%d", &x);
        if (x < 0)
            break;
        insert(x);
    }
    printf("数据输入完成:");
    Print();
    printf("请输入要删除的数据:");
    scanf("%d", &x);
    dele(x);
    printf("数据已删除:");
    Print();
    return 0;
}