鱼C论坛

 找回密码
 立即注册
查看: 1119|回复: 1

[已解决]关于删除链表中的元素失效的问题

[复制链接]
发表于 2021-11-19 00:24:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 GodLordGee 于 2021-11-19 00:28 编辑

在不看小甲鱼的视频前提下,想要自己实现链表的删除功能
目前遇到了一个问题,就是我在删除元素的函数里面定义了一个指针结构体temp,然后让这个指针结构体指向头指针head,然后我通过temp执行了删除操作
在函数中打印,可以看到temp中我指定的元素都被我删除了,但是当返回主程序打印head时,发现head中的元素没有改变,是哪里出问题了吗?
以下是输出实例
请直接输入数据?-1退出:1
1  当前head中的数值是:
1  请直接输入数据?-1退出:1
1  1  当前head中的数值是:
1  1  请直接输入数据?-1退出:2
1  1  2  当前head中的数值是:
1  1  2  请直接输入数据?-1退出:999
开始删除。。。请输入要删除的数值:1
要删除的原始链表是:1  1  2  
当前temp中的数值是:1  2
当前temp中的数值是:2
--=-=-=-=
................
当前head中的数值是:1  1  2
可以看到,temp中正确删除了数据,但是head没有改变。

以下是代码,在执行完输入数据操作后,输入999可以开始进行删除的操作。请重点看看delet函数。
#include <stdio.h>
#include <stdlib.h>

struct Node{
    int value;
    struct Node *next;
};

void insertNum(struct Node **head, int nums){
    struct Node *insertValue;
    struct Node *prior;
    struct Node *current;
    current = *head;
    prior = NULL;
    insertValue = (struct Node *)malloc(sizeof(struct Node));
    // insertValue->value = nums;
    
    while(current != NULL && current->value <nums){
        prior = current;
        current = current->next;
    }
    if(insertValue == NULL){
        printf("分配失败\n");
        exit(1);
    }
    insertValue->value = nums;
    insertValue->next = current;

    if(prior == NULL){
        *head = insertValue;
    }
    else{
        prior->next = insertValue;
    }
}

void printNum(struct Node **head){
    struct Node *tmp;
    tmp = *head;
    while(tmp!=NULL){
        printf("%d  ", tmp->value);
        tmp = tmp->next;
    }
}

void delet(struct Node **head, int num){
    struct Node *temp;
    struct Node *prior;
    temp = *head;
    prior = NULL;
    printf("要删除的原始链表是:");
    printNum(head);
    putchar('\n');
    while(temp != NULL){
        if(temp->value == num && prior == NULL){
            temp = temp->next;
            printf("当前temp中的数值是:");
            printNum(&temp);
            putchar('\n');
        }
        else if(temp->value == num && prior != NULL){
            prior->next = temp->next;
            temp = prior;
            printf("-----------------------------");
            prior = temp;
            temp = temp->next;
            printf("^^^^^^^^^^^^^^^^^^^^^%p\n", temp);
        }
        // printf("tttttttttttt");
        else{
            temp = temp->next;
            printf("--=-=-=-=\n");
            printNum(&temp);
            printf("................\n");
        }
    }

}

int main(void){
    struct Node *head = NULL;
    int nums;
    while(1){
        printf("请直接输入数据?-1退出:");
        scanf("%d", &nums);
        if (nums != -1 && nums!= 999){
            insertNum(&head, nums);
            printNum(&head);
        }
        else if(nums == 999){
            int del_num;
            printf("开始删除。。。请输入要删除的数值:");
            scanf("%d", &del_num);
            delet(&head, del_num);
            printf("当前head中的数值是:");
            printNum(&head);
            break;
        }
        printf("当前head中的数值是:\n");
        printNum(&head);
        
    }
}
最佳答案
2021-11-19 06:21:50
本帖最后由 jhq999 于 2021-11-19 06:45 编辑
void delet(struct Node **head, int num){
    struct Node *temp;
    struct Node *prior;
    temp = *head;
    prior = NULL;
    printf("要删除的原始链表是:");
    printNum(head);
    putchar('\n');
    while(temp != NULL)
    {
        if(temp->value == num )
       {
            printf("当前temp中的数值是:");
            printNum(&temp);
            putchar('\n');
            if(NULL==prior)////////符合条件的是表头
                  *head=temp->next;
            else                ////////符合条件的不是表头
                 prior->next=temp->next;
            free(temp);
            break;/////跳出循环
        }
        prior=temp;
        temp=temp->next;
    }
    //temp=NULL,链表没有符合条件的

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-19 06:21:50 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-11-19 06:45 编辑
void delet(struct Node **head, int num){
    struct Node *temp;
    struct Node *prior;
    temp = *head;
    prior = NULL;
    printf("要删除的原始链表是:");
    printNum(head);
    putchar('\n');
    while(temp != NULL)
    {
        if(temp->value == num )
       {
            printf("当前temp中的数值是:");
            printNum(&temp);
            putchar('\n');
            if(NULL==prior)////////符合条件的是表头
                  *head=temp->next;
            else                ////////符合条件的不是表头
                 prior->next=temp->next;
            free(temp);
            break;/////跳出循环
        }
        prior=temp;
        temp=temp->next;
    }
    //temp=NULL,链表没有符合条件的

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 01:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表