鱼C论坛

 找回密码
 立即注册
查看: 628|回复: 7

[已解决]程序无法完成删除操作,我返回的return head我感觉不是首地址

[复制链接]
发表于 2022-3-13 16:24:13 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#include<string.h>

typedef char DataType;

typedef struct ListNode
{
        DataType name[20];
        int grade;
        struct ListNode* next;
}ListNode;

struct ListNode* creat()
{
        ListNode* p1, * p2, * head;
        int n;
        p1 = p2 = (ListNode*)malloc(sizeof(ListNode));
        printf("please input name:  ");
        scanf("%s", p1->name);
        printf("grade : ");
        scanf("%d", &p1->grade);
        head = NULL;
        n = 0;
        while (p1->grade)
        {
                n++;
                if (1 == n)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;
                p1 = (struct ListNode*)malloc(sizeof(ListNode));
                printf("please input name:  ");
                scanf("%s", p1->name);
                printf("grade : ");
                scanf("%d", &p1->grade);
        }
        p2->next = NULL;
        return head;
}
ListNode* printNode(ListNode* head1)
{
        ListNode* head;
        head = head1;
        printf("开始打印:  \n");
        do
        {
                printf("%s\n", head->name);
                printf("%d\n", head->grade);
                head = head->next;
        } while (head);
        return head1;
}
struct ListNode* DelNode(struct ListNode* head, DataType x[20])
{
        ListNode* p1, * p2;
        if (head == NULL)
        {
                printf("the list is null\n");
                exit(-1);
        }
        p1 = p2 = head;
        while (strcmp(p1->name, x) != 0 && p1->next != NULL)
        {
                p2 = p1;
                p1 = p1->next;
        }
        if (strcmp(p1->name, x) == 0)
        {
                if (p1 = head)
                {
                        head = p1->next;
                }
                else
                {
                        p2->next = p1->next;
                }
        }
        return head;
}

int main()
{
        DataType delname[20];
//        char delname[20];
        ListNode* head;
        head = creat();
        printf("请输入要删除的元素姓名 : \n");
        scanf("%s", delname);
        head = DelNode(head, delname);
        printf("开始打印:  \n");
        head = printNode(head);
        return 0;
}
最佳答案
2022-3-13 18:28:53
抱歉兄弟,我不擅长修改其他人的代码(可能太长),所以我重新写了,你试试:
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #define T char

  6. // 单链表结构体
  7. typedef struct Node {
  8.         T name[20];
  9.         int grade;
  10.         struct Node* next;
  11. }ListNode;

  12. // 输入
  13. ListNode* insert(ListNode* head) {
  14.         ListNode* tail = head;
  15.         head = (ListNode*)malloc(sizeof(ListNode));
  16.         if (head) {
  17.                 printf("please input a name: ");
  18.                 if(scanf("%s", head->name))
  19.                         ;
  20.                 if(getchar())
  21.                         ;
  22.                 printf("please input a grade: ");
  23.                 if(scanf("%d", &head->grade))
  24.                         ;
  25.                 if (getchar())
  26.                         ;
  27.                 head->next = tail;
  28.         }
  29.         else printf("head is NULL\n");
  30.         return head;
  31. }

  32. // 删除
  33. ListNode* delete(ListNode* head, T name[20]) {
  34.         ListNode* prev, * now;
  35.         prev = now = head;
  36.         if (head) {
  37.                 while (head) {
  38.                         if (!(strcmp(now->name, name))) break;
  39.                         prev = now;
  40.                         now = now->next;
  41.                 }
  42.                 prev->next = now->next;
  43.                 free(now);
  44.                 return head;
  45.         }
  46.         else printf("head is NULL\n");
  47.         return NULL;
  48. }

  49. // 打印
  50. void print(ListNode* head) {
  51.         if (head) {
  52.                 while (head) {
  53.                         printf("name: %s, grade: %d\n", head->name, head->grade);
  54.                         head = head->next;
  55.                 }
  56.         }
  57.         else printf("head is NULL\n");
  58. }

  59. ListNode* FREE_ALL(ListNode* head) {
  60.         if (head) {
  61.                 while (head) {
  62.                         ListNode* p = head;
  63.                         head = head->next;
  64.                         free(p);
  65.                 }
  66.                 return head;
  67.         }
  68.         else printf("head is NULL\n");
  69.         return NULL;
  70. }

  71. int main() {
  72.         ListNode* head = NULL;
  73.         int N = 3; // 假设想输入 3 个人
  74.         T name[20];
  75.         for (int n = 0; n < N; n++) {
  76.                 head = insert(head);
  77.         }

  78.         print(head); // 打印单链表
  79.         printf("Please enter the name you want to delete: ");
  80.         if (scanf("%s", name))
  81.                 ;
  82.         head = delete(head, name); // 删除个资
  83.         print(head);  // 打印单链表
  84.         head = FREE_ALL(head); // 释放内存
  85.         free(head); // 释放内存
  86.         return 0;
  87. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-13 16:30:45 | 显示全部楼层
我在printf是head指针也会改变位置,那它不就返回的不是头指针了吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-13 16:48:46 | 显示全部楼层
sunjiam 发表于 2022-3-13 16:30
我在printf是head指针也会改变位置,那它不就返回的不是头指针了吗

只要不返回不就完事?基本上你的函数 printNode() 用来打印单链表,只需传入头指针便可,无需返回啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-13 16:54:40 | 显示全部楼层
傻眼貓咪 发表于 2022-3-13 16:48
只要不返回不就完事?基本上你的函数 printNode() 用来打印单链表,只需传入头指针便可,无需返回啊

#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#pragma warning(disable:4703)
#include<string.h>

typedef char DataType;

typedef struct ListNode
{
        DataType name[20];
        int grade;
        struct ListNode* next;
}ListNode;

struct ListNode* creat()
{
        ListNode* p1, * p2, * head;
        int n;
        p1 = p2 = (ListNode*)malloc(sizeof(ListNode));
        printf("please input name:  ");
        scanf("%s", p1->name);
        printf("grade : ");
        scanf("%d", &p1->grade);
        head = NULL;
        n = 0;
        while (p1->grade)
        {
                n++;
                if (1 == n)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;
                p1 = (struct ListNode*)malloc(sizeof(ListNode));
                printf("please input name:  ");
                scanf("%s", p1->name);
                printf("grade : ");
                scanf("%d", &p1->grade);
        }
        p2->next = NULL;
        return head;
}
void printNode(ListNode* head)
{
        printf("开始打印:  \n");
        do
        {
                printf("%s\n", head->name);
                printf("%d\n", head->grade);
                head = head->next;
        } while (head);
}
void DelNode(struct ListNode* head, int grade)
{
        ListNode* p1, * p2;
        if (head == NULL)
        {
                printf("the list is null\n");
                exit(-1);
        }
        p1 = head;
        while (p1->grade != grade && p1->next != NULL)
        {
                p2 = p1;
                p1 = p1->next;
        }
        if (p1->grade == grade)
        {
                if (p1 = head)
                {
                        head = p1->next;
                }
                else
                {
                        p2->next = p1->next;
                }
        }
}

int main()
{
        int grade;
//        char delname[20];
        ListNode* head;
        head = creat();
        printf("请输入要删除的元素姓名 : \n");
        scanf("%d", &grade);
        DelNode(head, grade);
        printf("开始打印:  \n");
        printNode(head);
        return 0;
}

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

使用道具 举报

 楼主| 发表于 2022-3-13 16:55:31 | 显示全部楼层
傻眼貓咪 发表于 2022-3-13 16:48
只要不返回不就完事?基本上你的函数 printNode() 用来打印单链表,只需传入头指针便可,无需返回啊

我删除不了啊,我看小甲鱼将的时候就返回return head;
我有点懵
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-13 17:04:54 | 显示全部楼层
please input name:  小狗
grade : 99
please input name:  小猫
grade : 98
please input name:  小姐
grade : 65
please input name:  小弟
grade : 888
please input name:  小节
grade : 666
please input name:  AD
grade : 0
请输入要删除的元素成绩 :
888
开始打印:
开始打印:
小狗
99
小猫
98
小姐
65
小弟
888
小节
666

C:\Users\lenovo\OneDrive\桌面\ly\链表1\x64\Debug\链表1.exe (进程 8344)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-13 18:28:53 | 显示全部楼层    本楼为最佳答案   
抱歉兄弟,我不擅长修改其他人的代码(可能太长),所以我重新写了,你试试:
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #define T char

  6. // 单链表结构体
  7. typedef struct Node {
  8.         T name[20];
  9.         int grade;
  10.         struct Node* next;
  11. }ListNode;

  12. // 输入
  13. ListNode* insert(ListNode* head) {
  14.         ListNode* tail = head;
  15.         head = (ListNode*)malloc(sizeof(ListNode));
  16.         if (head) {
  17.                 printf("please input a name: ");
  18.                 if(scanf("%s", head->name))
  19.                         ;
  20.                 if(getchar())
  21.                         ;
  22.                 printf("please input a grade: ");
  23.                 if(scanf("%d", &head->grade))
  24.                         ;
  25.                 if (getchar())
  26.                         ;
  27.                 head->next = tail;
  28.         }
  29.         else printf("head is NULL\n");
  30.         return head;
  31. }

  32. // 删除
  33. ListNode* delete(ListNode* head, T name[20]) {
  34.         ListNode* prev, * now;
  35.         prev = now = head;
  36.         if (head) {
  37.                 while (head) {
  38.                         if (!(strcmp(now->name, name))) break;
  39.                         prev = now;
  40.                         now = now->next;
  41.                 }
  42.                 prev->next = now->next;
  43.                 free(now);
  44.                 return head;
  45.         }
  46.         else printf("head is NULL\n");
  47.         return NULL;
  48. }

  49. // 打印
  50. void print(ListNode* head) {
  51.         if (head) {
  52.                 while (head) {
  53.                         printf("name: %s, grade: %d\n", head->name, head->grade);
  54.                         head = head->next;
  55.                 }
  56.         }
  57.         else printf("head is NULL\n");
  58. }

  59. ListNode* FREE_ALL(ListNode* head) {
  60.         if (head) {
  61.                 while (head) {
  62.                         ListNode* p = head;
  63.                         head = head->next;
  64.                         free(p);
  65.                 }
  66.                 return head;
  67.         }
  68.         else printf("head is NULL\n");
  69.         return NULL;
  70. }

  71. int main() {
  72.         ListNode* head = NULL;
  73.         int N = 3; // 假设想输入 3 个人
  74.         T name[20];
  75.         for (int n = 0; n < N; n++) {
  76.                 head = insert(head);
  77.         }

  78.         print(head); // 打印单链表
  79.         printf("Please enter the name you want to delete: ");
  80.         if (scanf("%s", name))
  81.                 ;
  82.         head = delete(head, name); // 删除个资
  83.         print(head);  // 打印单链表
  84.         head = FREE_ALL(head); // 释放内存
  85.         free(head); // 释放内存
  86.         return 0;
  87. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-14 09:22:00 | 显示全部楼层
傻眼貓咪 发表于 2022-3-13 18:28
抱歉兄弟,我不擅长修改其他人的代码(可能太长),所以我重新写了,你试试:

我找见自己的错误了,也谢谢你提供的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 19:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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