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

题目内容: 实现两个由单项链表存储的有序字母数据的合并,如有重复的则只保留一个。 例如:给定{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

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

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632