鱼C论坛

 找回密码
 立即注册
查看: 1310|回复: 3

[已解决]这个删除结点的函数哪里有问题,当他要删除第一个节点时就会出错

[复制链接]
发表于 2021-9-13 17:21:44 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node* node;
  4. struct Node {
  5.     int data;
  6.     node next;
  7. };
  8. node CreateList()
  9. {
  10.     int num;
  11.     node head=NULL;
  12.     node tail,p;//p指向新申请的节点,tail指向尾巴
  13.     scanf("%d",&num);
  14.     if(num==-1)
  15.         return head;
  16.     else
  17.     {
  18.         p=(node)malloc(sizeof(struct Node));
  19.         head = p;
  20.     }
  21.     while(num!=-1)
  22.     {
  23.         tail=p;
  24.         tail->data=num;//读入数据
  25.         p = (node)malloc(sizeof(struct Node));
  26.         tail->next = p;
  27.         scanf("%d",&num);
  28.     }
  29.     tail->next=NULL;//尾巴指向NULL标志结束
  30.     return head;
  31. }
  32. void PrintList(node head)
  33. {
  34.     node p;
  35.     for(p=head;p!=NULL;p=p->next)
  36.         printf("\t%d",p->data);
  37. }
  38. void DeleteData(node head,int m)//删除指定结点
  39. {
  40.     if(head)//链表非空
  41.     {
  42.         node p=head,temp;
  43.         while(p->data!=m&&p!=NULL)//找到待删除的结点的位置
  44.         {
  45.             temp=p;//temp永远指向p的前一个位置
  46.             p=p->next;
  47.         }
  48.         if(p==head)//如果删除的是头节点
  49.         {
  50.             head=p->next;
  51.             free(p);
  52.         }
  53.        else if(p->data==m)
  54.         {
  55.            temp->next=p->next;
  56.            free(p);
  57.         }
  58.        else
  59.         {
  60.            printf("没找到,草\n");
  61.         }
  62.     }
  63.     else//链表为空
  64.     {
  65.         printf("链表为空\n");
  66.     }
  67. }
  68. int main()
  69. {
  70.     int num;
  71.     int n;
  72.     node head=NULL;
  73.     head = CreateList();
  74.     PrintList(head);
  75.     scanf("%d",&n);
  76.     DeleteData(head,n);
  77.     PrintList(head);
  78.     return 0;
  79. }
复制代码
最佳答案
2021-9-13 20:15:30
本帖最后由 jhq999 于 2021-9-13 20:33 编辑

在函数定义和调用时,有三种常见的参数传递方式:

1.传值

2.传指针

3.传引用
  1. #include "stdafx.h"
  2. #include<stdlib.h>
  3. typedef struct Node* node;
  4. struct Node {
  5.     int data;
  6.     node next;
  7. };
  8. node CreateList()
  9. {
  10.     int num;
  11.     node head=NULL;
  12.     node tail=NULL,p=NULL;//p指向新申请的节点,tail指向尾巴
  13.     scanf("%d",&num);
  14.     if(num==-1)
  15.         return head;
  16.     else
  17.     {
  18.         p=(node)malloc(sizeof(struct Node));
  19.         head = p;
  20.     }
  21.     while(num!=-1)
  22.     {
  23.         tail=p;
  24.         tail->data=num;//读入数据
  25.         p = (node)malloc(sizeof(struct Node));
  26.         tail->next = p;
  27.         scanf("%d",&num);
  28.     }
  29.     tail->next=NULL;//尾巴指向NULL标志结束
  30.     return head;
  31. }
  32. void PrintList(node head)
  33. {
  34.     node p;
  35.     for(p=head;p!=NULL;p=p->next)
  36.         printf("\t%d",p->data);
  37. }
  38. void DeleteData(node *phead,int m)//删除指定结点             你传的是指针的值,你得传指针的地址,否则出函数后head不会变成next还是原来的,或者用引用void DeleteData(node &head,int m)
  39. {
  40.         node head=*phead;//////////////////////////////
  41.     if(head)//链表非空
  42.     {
  43.         node p=head,temp=NULL;
  44.         while(p->data!=m&&p!=NULL)//找到待删除的结点的位置
  45.         {
  46.             temp=p;//temp永远指向p的前一个位置
  47.             p=p->next;
  48.         }
  49.         if(p==head)//如果删除的是头节点
  50.         {
  51.             head=p->next;
  52.             free(p);
  53.         }
  54.        else if(p->data==m)
  55.         {
  56.            temp->next=p->next;
  57.            free(p);
  58.         }
  59.        else
  60.         {
  61.            printf("没找到,草\n");
  62.         }
  63.     }
  64.     else//链表为空
  65.     {
  66.         printf("链表为空\n");
  67.     }
  68.     *phead=head;///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. }
  70. int main()
  71. {
  72.     int num;
  73.     int n;
  74.     node head=NULL;
  75.     head = CreateList();
  76.     PrintList(head);
  77.     scanf("%d",&n);
  78.     DeleteData(&head,n);
  79.     PrintList(head);
  80.     return 0;
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-13 19:16:16 | 显示全部楼层
C:\Users\YJ\Desktop\QQ截图20210913191416.jpg我这有用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-13 20:15:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-9-13 20:33 编辑

在函数定义和调用时,有三种常见的参数传递方式:

1.传值

2.传指针

3.传引用
  1. #include "stdafx.h"
  2. #include<stdlib.h>
  3. typedef struct Node* node;
  4. struct Node {
  5.     int data;
  6.     node next;
  7. };
  8. node CreateList()
  9. {
  10.     int num;
  11.     node head=NULL;
  12.     node tail=NULL,p=NULL;//p指向新申请的节点,tail指向尾巴
  13.     scanf("%d",&num);
  14.     if(num==-1)
  15.         return head;
  16.     else
  17.     {
  18.         p=(node)malloc(sizeof(struct Node));
  19.         head = p;
  20.     }
  21.     while(num!=-1)
  22.     {
  23.         tail=p;
  24.         tail->data=num;//读入数据
  25.         p = (node)malloc(sizeof(struct Node));
  26.         tail->next = p;
  27.         scanf("%d",&num);
  28.     }
  29.     tail->next=NULL;//尾巴指向NULL标志结束
  30.     return head;
  31. }
  32. void PrintList(node head)
  33. {
  34.     node p;
  35.     for(p=head;p!=NULL;p=p->next)
  36.         printf("\t%d",p->data);
  37. }
  38. void DeleteData(node *phead,int m)//删除指定结点             你传的是指针的值,你得传指针的地址,否则出函数后head不会变成next还是原来的,或者用引用void DeleteData(node &head,int m)
  39. {
  40.         node head=*phead;//////////////////////////////
  41.     if(head)//链表非空
  42.     {
  43.         node p=head,temp=NULL;
  44.         while(p->data!=m&&p!=NULL)//找到待删除的结点的位置
  45.         {
  46.             temp=p;//temp永远指向p的前一个位置
  47.             p=p->next;
  48.         }
  49.         if(p==head)//如果删除的是头节点
  50.         {
  51.             head=p->next;
  52.             free(p);
  53.         }
  54.        else if(p->data==m)
  55.         {
  56.            temp->next=p->next;
  57.            free(p);
  58.         }
  59.        else
  60.         {
  61.            printf("没找到,草\n");
  62.         }
  63.     }
  64.     else//链表为空
  65.     {
  66.         printf("链表为空\n");
  67.     }
  68.     *phead=head;///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. }
  70. int main()
  71. {
  72.     int num;
  73.     int n;
  74.     node head=NULL;
  75.     head = CreateList();
  76.     PrintList(head);
  77.     scanf("%d",&n);
  78.     DeleteData(&head,n);
  79.     PrintList(head);
  80.     return 0;
  81. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-15 20:05:00 | 显示全部楼层
jhq999 发表于 2021-9-13 20:15
在函数定义和调用时,有三种常见的参数传递方式:

1.传值

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 14:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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