user-0 提问: 有大佬知道这个是什么问题吗

回复:
user: user-0
content: 引用 8 楼 chxchxkkk 的回复:你这个malloc只给了一个结点分配了内存空间。添加结点要在循环语句里使用malloc分配 谢谢大佬指点，我现在给啦他循环。 #include<stdio.h> #include<stdlib.h> #include<string.h> #pragma warning(disable:4996) typedef struct stu { 	int num; 	int score; 	struct stu* next; }student; int main() { 	int m, n; 	scanf("%d %d", &m, &n); 	int x = m; 	int y = n; 	student* p1; 	student* p2; 	student* head1 = (student*)malloc(sizeof(student));         	student* list1 = (student*)malloc(sizeof(student)); 	student* head2 = (student*)malloc(sizeof(student)); 	student* list2 = (student*)malloc(sizeof(student)); 	p1 = head1; 	p2 = head2; 	for (int i = 0;i < m;i++) { 		scanf("%d %d", &list1->num, &list1->score); 		list1 = (student*)malloc(sizeof(student)); 		p1->next = list1; 		p1 = list1; 	} 	list1->next = NULL;           //第一个链表尾结点的next是NULL 	p1 = head1;                //p1重新指向头结点 	for (int i = 0;i < n;i++) { 		scanf("%d %d", &list2->num, &list2->score); 		list2 = (student*)malloc(sizeof(student)); 		p2->next = list2; 		p2 = list2; 	} 	list2->next = NULL;             //第二个链表尾结点的next是NULL 	p2 = head2->next;                     //p2指向头结点的next，即第一个元素 	list1->next = p2;   //第一个链表的尾结点的next是第二个链表的第一个元素，这时候合起来的链表应该是只有一个头结点，其他是x+y个元素，最后一个结点的next是NULL 	printf("\n\n\n"); 	int sum = x+y; 	while(sum--) 	{ 		p1 = p1->next; 		printf("%d %d\n", p1->num, p1->score);         //p1从头结点开始循环，输出各个元素的成员的值 	} 	return 0; } 之前的问题都解决啦，我把我刚才的做啦修改，但是最后输出的时候，它输出少啦2个链表的第一个元素，然后多了2个链表尾结点的next，就是空的元素。

user: user-1
content: 你这个malloc只给了一个结点分配了内存空间。添加结点要在循环语句里使用malloc分配

user: user-2
content: #include<stdio.h> #include<stdlib.h> #pragma warning(disable:4996) typedef struct stu {     int num;     int score;     struct stu* next; }student;   void create_link(student **pphead, int num) {     student *pcur, *pnext;     int i = 0;      while (i < num) {         pcur = (student *)malloc(sizeof(student));         if (!pcur)             exit(0);         scanf("%d %d", &pcur->num, &pcur->score);         if (!*pphead) {             *pphead = pcur;             pnext = *pphead;             i++;             continue;         }         pnext->next = pcur;         pnext = pcur;         i++;     }     pnext->next = NULL; }  void print_link(student *phead) {     student *pcur = phead;      while (pcur) {         printf("%d %d\n", pcur->num, pcur->score);         pcur = pcur->next;     } }  int main() {     int m, n, y;      student *head1, *head2, *pcur;      head1 = head2 = NULL;      scanf("%d %d", &m, &n);     create_link(&head1, m);     //print_link(head1);     create_link(&head2, n);     //print_link(head2);      y = m + n;      pcur = head1;     while (pcur && pcur->next)         pcur = pcur->next;      pcur->next = head2;       print_link(head1);   #if 0     student* head1, * list1, * head2, * list2, * p1, * p2;     head1 = list1 = head2 = list2 = (student*)malloc(sizeof(student));     if (!head1)         exit(0);     p1 = head1;     p2 = head2;     p1->next = NULL;     while(m--) {         scanf("%d %d", &list1->num, &list1->score);         list1->next = NULL;         p1->next = list1;         p1 = list1;         list1 = (student*)malloc(sizeof(student));         if (!list1)             exit(0);     }     free(list1);    //最后一个list1没有执行scanf      p2 = head1;     list2 = (student*)malloc(sizeof(student));     if (!list2)         exit(0);     while (n--) {         scanf("%d %d", &list2->num, &list2->score);         list2->next = p2->next;         p2->next = list2;         p2 = list2;         list2 = (student*)malloc(sizeof(student));         if (!list2)             exit(0);     }     free(list2);    //最后一个list1没有执行scanf     /*     p2 = head2->next;     list1->next = p2;     */     p1 = head1;     int y = m + n;     while(y--)     {         printf("%d %d", p1->num, p1->score);         p1 = p1->next;     } #endif     return 0; } 供参考～   楼主的代码看不出逻辑所在，不申请空间怎么赋值？

user: user-0
content: 我把它们4个都分开操作啦，最后没有出现说我p1是NULL啦，但是它输出第一个和第二个链表的最后一个元素后，第三个就输出不了，错误位置还是在那个地方

user: user-3
content: 你那个四连指向同一内存位置的操作,可能是原因吧,总感觉怪怪的

user: user-0
content: 那张图发错啦，看代码吧

user: user-0
content: #include<stdio.h> #include<stdlib.h> #pragma warning(disable:4996) typedef struct stu { 	int num; 	int score; 	struct stu* next; }student; int main() { 	int m, n; 	scanf("%d %d", &m, &n); 	student* head1, * list1, * head2, * list2, * p1, * p2; 	head1 = list1 = head2 = list2 = (student*)malloc(sizeof(student)); 	p1 = head1; 	p2 = head2; 	while(m--) { 		scanf("%d %d", &list1->num, &list1->score); 		p1->next = list1; 		list1->next = NULL; 		p1 = list1; 	} 	p1 = head1; 	while (n--) { 		scanf("%d %d", &list2->num, &list2->score); 		p2->next = list2; 		list2->next = NULL; 		p2 = list2; 	} 	p2 = head2->next; 	list1->next = p2; 	int y = m + n; 	while(y--) 	{ 		p1 = p1->next; 		printf("%d %d", p1->num, p1->score); 	} 	return 0; } 这是源代码

user: user-2
content: while在做什么？一直再给同一个节点赋值呀

user: user-2
content: 直接发源码吧，不建议发图片

user: user-0
content: 改进后的输出是这样子的，感觉是把2个链表后移一步然后合并在一起，但是我看不出代码哪里出问题啦

user: user-0
content: 引用 10 楼 chxchxkkk 的回复:while(sum--) { p1 = p1->next; printf("%d %d\n", p1->num, p1->score);         //p1从头结点开始循环，输出各个元素的成员的值 } 一般输出链表这样： while(p1->next != NULL) {     printf("%d %d\n", p1->num, p1->score);         //p1从头结点开始循环，输出各个元素的成员的值     p1 = p1->next; } 谢谢提醒，但是最后输出还是有问题，不是这个的原因

user: user-1
content: while(sum--) { p1 = p1->next; printf("%d %d\n", p1->num, p1->score);         //p1从头结点开始循环，输出各个元素的成员的值 } 一般输出链表这样： while(p1->next != NULL) {     printf("%d %d\n", p1->num, p1->score);         //p1从头结点开始循环，输出各个元素的成员的值     p1 = p1->next; }