鱼C论坛

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

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

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

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

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

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

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

可以看到,temp中正确删除了数据,但是head没有改变。

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

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

  3. struct Node{
  4.     int value;
  5.     struct Node *next;
  6. };

  7. void insertNum(struct Node **head, int nums){
  8.     struct Node *insertValue;
  9.     struct Node *prior;
  10.     struct Node *current;
  11.     current = *head;
  12.     prior = NULL;
  13.     insertValue = (struct Node *)malloc(sizeof(struct Node));
  14.     // insertValue->value = nums;
  15.    
  16.     while(current != NULL && current->value <nums){
  17.         prior = current;
  18.         current = current->next;
  19.     }
  20.     if(insertValue == NULL){
  21.         printf("分配失败\n");
  22.         exit(1);
  23.     }
  24.     insertValue->value = nums;
  25.     insertValue->next = current;

  26.     if(prior == NULL){
  27.         *head = insertValue;
  28.     }
  29.     else{
  30.         prior->next = insertValue;
  31.     }
  32. }

  33. void printNum(struct Node **head){
  34.     struct Node *tmp;
  35.     tmp = *head;
  36.     while(tmp!=NULL){
  37.         printf("%d  ", tmp->value);
  38.         tmp = tmp->next;
  39.     }
  40. }

  41. void delet(struct Node **head, int num){
  42.     struct Node *temp;
  43.     struct Node *prior;
  44.     temp = *head;
  45.     prior = NULL;
  46.     printf("要删除的原始链表是:");
  47.     printNum(head);
  48.     putchar('\n');
  49.     while(temp != NULL){
  50.         if(temp->value == num && prior == NULL){
  51.             temp = temp->next;
  52.             printf("当前temp中的数值是:");
  53.             printNum(&temp);
  54.             putchar('\n');
  55.         }
  56.         else if(temp->value == num && prior != NULL){
  57.             prior->next = temp->next;
  58.             temp = prior;
  59.             printf("-----------------------------");
  60.             prior = temp;
  61.             temp = temp->next;
  62.             printf("^^^^^^^^^^^^^^^^^^^^^%p\n", temp);
  63.         }
  64.         // printf("tttttttttttt");
  65.         else{
  66.             temp = temp->next;
  67.             printf("--=-=-=-=\n");
  68.             printNum(&temp);
  69.             printf("................\n");
  70.         }
  71.     }

  72. }

  73. int main(void){
  74.     struct Node *head = NULL;
  75.     int nums;
  76.     while(1){
  77.         printf("请直接输入数据?-1退出:");
  78.         scanf("%d", &nums);
  79.         if (nums != -1 && nums!= 999){
  80.             insertNum(&head, nums);
  81.             printNum(&head);
  82.         }
  83.         else if(nums == 999){
  84.             int del_num;
  85.             printf("开始删除。。。请输入要删除的数值:");
  86.             scanf("%d", &del_num);
  87.             delet(&head, del_num);
  88.             printf("当前head中的数值是:");
  89.             printNum(&head);
  90.             break;
  91.         }
  92.         printf("当前head中的数值是:\n");
  93.         printNum(&head);
  94.         
  95.     }
  96. }
复制代码
最佳答案
2021-11-19 06:21:50
本帖最后由 jhq999 于 2021-11-19 06:45 编辑
  1. void delet(struct Node **head, int num){
  2.     struct Node *temp;
  3.     struct Node *prior;
  4.     temp = *head;
  5.     prior = NULL;
  6.     printf("要删除的原始链表是:");
  7.     printNum(head);
  8.     putchar('\n');
  9.     while(temp != NULL)
  10.     {
  11.         if(temp->value == num )
  12.        {
  13.             printf("当前temp中的数值是:");
  14.             printNum(&temp);
  15.             putchar('\n');
  16.             if(NULL==prior)////////符合条件的是表头
  17.                   *head=temp->next;
  18.             else                ////////符合条件的不是表头
  19.                  prior->next=temp->next;
  20.             free(temp);
  21.             break;/////跳出循环
  22.         }
  23.         prior=temp;
  24.         temp=temp->next;
  25.     }
  26.     //temp=NULL,链表没有符合条件的

  27. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-19 06:21:50 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-11-19 06:45 编辑
  1. void delet(struct Node **head, int num){
  2.     struct Node *temp;
  3.     struct Node *prior;
  4.     temp = *head;
  5.     prior = NULL;
  6.     printf("要删除的原始链表是:");
  7.     printNum(head);
  8.     putchar('\n');
  9.     while(temp != NULL)
  10.     {
  11.         if(temp->value == num )
  12.        {
  13.             printf("当前temp中的数值是:");
  14.             printNum(&temp);
  15.             putchar('\n');
  16.             if(NULL==prior)////////符合条件的是表头
  17.                   *head=temp->next;
  18.             else                ////////符合条件的不是表头
  19.                  prior->next=temp->next;
  20.             free(temp);
  21.             break;/////跳出循环
  22.         }
  23.         prior=temp;
  24.         temp=temp->next;
  25.     }
  26.     //temp=NULL,链表没有符合条件的

  27. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 15:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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