解题思路:
注意事项:
参考代码:
#include<stdio.h>
#include<stdlib.h>
struct Student
{
int num;
int score;
};
struct Node
{
struct Student data;
struct Node* next;
};
struct Node* creatlist()
{
struct Node* headnode = (struct Node*)malloc(sizeof(struct Node));
headnode->next = NULL;
return headnode;
}
struct Node* creatnode(struct Student* data)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data.num = data->num;
newnode->data.score = data->score;
newnode->next = NULL;
return newnode;
}
void printlist(struct Node* head)
{
struct Node* pmove = head->next;
while (pmove)
{
printf("%d %d\n", pmove->data.num, pmove->data.score);
pmove = pmove->next;
}
}
void insertheadby(struct Node* head, struct Student* data)
{
struct Node* newNode = creatnode(data);
struct Node* posNode = head;
while (posNode->next != NULL)
{
posNode = posNode->next;
}
posNode->next = newNode;
}
//void deletenode(struct Node* head, int data)
//{
//struct Node* postNodeFront = head;
//struct Node* postNode = head->next;
//if (postNode == NULL)
//return;
//while (postNode->data != data)
//{
//postNodeFront = postNode;
//postNode = postNodeFront->next;
//if (postNode == NULL)
//return;
//}
//postNodeFront->next = postNode->next;
//deletenode(head, data);
//}
struct Node* merge(struct Node* head1, struct Node* head2)
{
struct Node* p, * q, * r;
p = head1->next; q = head2->next;
r = head1;//也可以选择开辟新的内存空间
while (1)
{
if (p != NULL && q != NULL)
{
if(p->data.num<q->data.num)
{
r->next = p;
r = p;
p = p->next;
}
else
{
r->next = q;
r = q; q = q->next;
}
}
else if (p == NULL)//任何一个单链表为null,就可以退出循环了
{
r->next = q;
break;
}
else if (q == NULL)
{
r->next = p;
break;
}
}
return head1;
}
void BubbleSort(struct Node* list)
{
for (struct Node* first = list->next; first != NULL; first = first->next)
{
for (struct Node* second = list->next; second != NULL; second = second->next)
{
if (second->next != NULL)
{
if (second->data.num > second->next->data.num)
{
struct Student temp = second->data;
second->data = second->next->data;
second->next->data= temp;
}
}
}
}
}
int main()
{
struct Node* head1 = creatlist();
struct Node* head2 = creatlist();
struct Student* data1 = (struct Student*)malloc(sizeof(struct Student));
int n, i,m;
scanf("%d%d", &n,&m);
for (i = 0; i < n; i++)
{
scanf("%d%d", &data1->num, &data1->score);
insertheadby(head1, data1);
}
for (i = 0; i < m; i++)
{
scanf("%d%d", &data1->num, &data1->score);
insertheadby(head2, data1);
}
struct Node *mergence=merge(head1, head2);
BubbleSort(mergence);
printlist(mergence);
return 0;
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复