两个单链表头尾相接不成功的问题,应该是低级错误,谢谢
想实现两个单链表电话本,先查找并删除重复项,然后头尾连接,总是失败,帮忙看下,谢谢程序代码:
void combine(node *head1, node *head2) //合并 { node *p1, *p2, *pre1 = NULL, *pre2 = NULL; //pre指向前驱结点 if (!head1 && !head2) { puts("两个电话本都是空的,合不了"); } else { p1 = head1; p2 = head2; while (p1) { while (p2) { while ((strcmp(p2->item.name,p1->item.name) != 0) || (p2->item.number != p1->item.number)) { pre2 = p2; p2 = p2->next; } if (p2->next) { if (!pre2) { p2 = p2->next; head2 = head2->next; } else { pre2->next = p2->next; } } else { p2 = head2; } } p2 = head2; pre1 = p1; p1 = p1->next; } pre1->next = head2; p1 = head1; display(p1); //打印 } }