69,373
社区成员
发帖
与我相关
我的任务
分享
#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;
}