I Am Rex 2021-05-23 23:42 采纳率: 0%
浏览 34

最后一个节点过不去,不知道什么原因

题目内容: 实现两个由单项链表存储的有序字母数据的合并,如有重复的则只保留一个。 例如:给定{a, c ,f}, { b, e, g}合并后结果为{a, b, c , e , f , g}。 输入格式: 两个有序字母数据 输出格式: 合并后的字母数据 输入样例1: a b c[回车] d e f[回车] 输出样例1: a b c d e f[回车] 输入样例2: e f g[回车] e g m[回车] 输出样例2: e f g m[回车] 我写的代码: #include struct Node { char ch; struct Node* Next; }; typedef struct Node N; void My_malloc(N**); N* My_combin(N*, N*); void My_output(N*); void My_disrepeat(N**); int main(void) { N* Heada = NULL; N* Headb = NULL; N* Headc = NULL; My_malloc(&Heada); My_malloc(&Headb); if (Heada == NULL && Headb == NULL) { return 0; } else if (Heada == NULL) { My_disrepeat(&Headb); My_output(Headb); return 0; } else if (Headb == NULL) { My_disrepeat(&Heada); My_output(Heada); return 0; } My_disrepeat(&Heada); My_disrepeat(&Headb); Headc = My_combin(Heada, Headb); My_output(Headc); return 0; } void My_malloc(N** H) { N* New = NULL; N* Current = NULL; char ch; while (scanf("%c", &ch) && ch != '\n') { if (ch != ' ') { New = (N*)malloc(sizeof(N)); New->ch = ch; New->Next = NULL; if (*H == NULL) { *H = New; Current = New; } else { Current->Next = New; Current = New; } } } } N* My_combin(N* A, N* B) { N* Currenta = A; N* CurrentN = NULL; N* Head = NULL; N* Currentb = B; N* New = NULL; int flag = 1; while (Currenta) { New = (N*)malloc(sizeof(N)); New->ch = Currenta->ch; New->Next = NULL; if (Head == NULL) { Head = New; CurrentN = New; } else { CurrentN->Next = New; CurrentN = New; } Currenta = Currenta->Next; } while (Currentb) { Currenta = A; flag = 1; while (Currenta) { if (Currentb->ch == Currenta->ch) { flag = 0; break; } Currenta = Currenta->Next; } if (flag) { New = (N*)malloc(sizeof(N)); New->ch = Currentb->ch; New->Next = NULL; CurrentN->Next = New; CurrentN = New; } Currentb = Currentb->Next; } return Head; } void My_output(N* H) { N* Current = H; while (Current) { printf("%c", Current->ch); Current = Current->Next; if (Current) { printf(" "); } else { printf("\n"); } } return; } void My_disrepeat(N** H) { N* Current = *H; N* Current2 = *H; N* NCurrent = NULL; N* NH = NULL; int flag = 1; while (Current) { flag = 1; Current2 = Current->Next; while (Current2) { if (Current2->ch == Current->ch) { flag = 0; break; } Current2 = Current2->Next; } if (flag) { if (NH == NULL) { NH = Current; NCurrent = Current; } else { NCurrent->Next = Current; NCurrent = Current; } } Current = Current->Next; } if (NCurrent) NCurrent->Next = NULL; *H = NH; } 用例测试结果 运行时间 占用内存 提示 得分 用例1通过 2ms 256kb 5 用例2通过 1ms 256kb 5 用例3未通过 0ms 0kb 结果错误 0

  • 写回答

2条回答 默认 最新

  • 关注

    把你的代码重新格式化一下,点击“代码段”图标上传。

     

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序