帮看一下这个(两个链表合并)代码的问题,谢谢大家了
// 7.12.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include"stdio.h"
#include"stdlib.h"
struct node{
int data;
struct node *next;
};
int _tmain(int argc, _TCHAR* argv[])
{
int n, x,n1;
bool flag = true;
struct node *p = NULL, *p2 = NULL, *p1 = NULL, *head = NULL, *head1 = NULL, *p3 = NULL,*p4=NULL,*p5=NULL;
printf("请输入第一个链表 输入0结束\n");
scanf_s("%d", &n);
while (n != 0)
{
p = (struct node *)malloc(10);
p->data = n;
if (head == NULL)head = p;
else p2->next = p;
p2 = p;
scanf_s("%d", &n);
}
printf("请输入第二个链表 输入0结束\n");
scanf_s("%d", &n1);
while (n1 != 0)
{
p1 = (struct node *)malloc(10);
p1->data = n1;
if (head1 == NULL)head1 = p1;
else p3->next = p1;
p3= p1;
scanf_s("%d", &n1);
}
p5 = head;
while (p5->next != NULL) //找尾结点
{
p5 = p5->next;
}
p5->next = head1; //让第一个链表的尾结点指向第二个的头结点,我觉得是这里出问题
p4= head;
while (p4->next != NULL) //完全没有进入循环。。。。
{
printf("sfdasdf");
printf("%d", p4->data);
p4 = p4->next;
}
}