LXTTTTTTTT 2022-02-03 20:32 采纳率: 66.7%
浏览 34
已结题

新学单链表,删除0的时候就会出现这样的输出

为什么在删除元素的时候,删除别的元素都没有问题,但删除0的时候就会出现这样的输出

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

struct Data
{
    int data;
};

struct Linked_Node
{
    struct Data data;
    struct Linked_Node* next;
};

struct Linked_Node* setNode(void);//创建链表
struct Linked_Node* createNode(int);//创建节点
void insertByHead(struct Linked_Node*,int);//插入节点(头插法)

void deleteNode(struct Linked_Node*,int);//删除节点(指定数据)
void printNode(struct Linked_Node*);//打印链表

struct Linked_Node* setNode(void)//创建链表
{
    struct Linked_Node* head = (struct Linked_Node*)malloc(sizeof(struct Linked_Node*));
    head->data.data = 0;
    head->next = NULL;
    return head;
}

struct Linked_Node* createNode(int data)//创建节点
{
    struct Linked_Node* node = (struct Linked_Node*)malloc(sizeof(struct Linked_Node*));
    node->data.data = data;
    node->next = NULL;
    return node;
}

void insertByHead(struct Linked_Node* head,int data)//插入节点(头插法)
{
    struct Linked_Node* node = createNode(data);
    node->next = head->next;
    head->next = node;
    return;
}

void deleteNode(struct Linked_Node* head,int data)//删除节点(指定数据)
{
    struct Linked_Node* tempnode = head;
    struct Linked_Node* tempnodeformer = tempnode;

    if(head->next == NULL){printf("链表为空\n"); return;}

    while(tempnode->data.data != data)
    {
        tempnodeformer = tempnode;
        tempnode = tempnodeformer->next;

        if(tempnode == NULL)
        {
        printf("链表无目标数据\n"); 
        return;
        }
    }

    tempnodeformer->next = tempnode->next;
    free(tempnode);
    return;
}

void printNode(struct Linked_Node* head)//打印链表
{
    struct Linked_Node* tempnode = head->next;
    if(head->next == NULL){printf("链表为空\n"); return;}

    do
    {
        printf("%d\t",tempnode->data.data);
        tempnode = tempnode->next;
    }while(tempnode->next != NULL);
    printf("%d\t",tempnode->data.data);
    return;
}

int main(void)
{
    struct Linked_Node* head = setNode();
    insertByHead(head,2);
    insertByHead(head,1);
    insertByHead(head,0);
    deleteNode(head,0);
    printNode(head);

    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • 关注

    因为你的head节点的data也是0,所以查找的时候把head节点给删掉了。
    另外,malloc申请空间的地方,都写错了
    (struct Linked_Node *)malloc(sizeof(struct Linked_Node *));应该是
    (struct Linked_Node *)malloc(sizeof(struct Linked_Node)); //最后sizeof里面没有星号

    img

    删除节点的代码修改如下:

    
    void deleteNode(struct Linked_Node* head,int data)//删除节点(指定数据)
    {
        struct Linked_Node* tempnode = head->next; //修改1 从head的下一个节点开始
        struct Linked_Node* tempnodeformer = head; //修改2
    
        //修改3
        if(head==NULL || head->next == NULL){printf("链表为空\n"); return;}
    
        while(tempnode->data.data != data)
        {
            tempnodeformer = tempnode;
            tempnode = tempnodeformer->next;
    
            if(tempnode == NULL)
            {
                printf("链表无目标数据\n"); 
                return;
            }
        }
    
        tempnodeformer->next = tempnode->next;
        free(tempnode);
        return;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月12日
  • 已采纳回答 2月4日
  • 创建了问题 2月3日

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图