解题思路:
注意事项:看别人代码真的很烦。排序抄大佬的。没什么注释,
参考代码:
#include <stdio.h>
#include <malloc.h>
struct Link{
int fraction;
int id;
struct Link *next;
};
typedef struct{
struct Link *head;
struct Link *last;
}List;
void add_head(List* list, int n);
void print_link(List* list, int n);
void connect_link(List* list, List* list2);
void sort_link(List* list);
void destory_Link(List *list);
void print_link2(List *list);
int main(){
List link;
List link2;
int n,m;
//printf("please input: ");
scanf("%d %d",&n,&m);
add_head(&link,n);
add_head(&link2,m);
connect_link(&link,&link2);
sort_link(&link);
print_link(&link,n+m);
destory_Link(&link);
return 0;
}
//链表结点排序重组
void sort_link(List* list)
{
if ( list->head == NULL)
return;
struct Link *pre = NULL;
struct Link *cur = NULL;
struct Link tmp;
for ( pre = list->head; pre->next != NULL; pre = pre->next)
{
for (cur = pre->next; cur != NULL; cur= cur->next)
{
if( pre->id > cur->id)
{
//数据域交换
tmp = *pre;
*pre = *cur;
*cur = tmp;
//指针域交换
tmp.next = pre->next;
pre->next = cur->next;
cur->next = tmp.next;
}
}
}
}
void connect_link(List* list, List* list2)
{
list->last->next = list2->head;
}
void print_link(List* list, int n)
{
int i;
for ( i=0; i<n; i++)
{
printf("%d %d\n",list->head->id,list->head->fraction);
list->head = list->head->next;
}
}
void print_link2(List *list)
{
if ( list->head == NULL || list->head->next == NULL)
{
//printf("invalid list\n");
return;
}
struct Link *cur = list->head;
while(cur != NULL)
{
printf("%d %d\n", cur->id, cur->fraction);
cur = cur->next;
}
}
void add_head(List* list, int n)
{
int i;
list->head = NULL;
//list->last = NULL;
struct Link *pnode = NULL;
for ( i=0; i<n; i++)
{
pnode = (struct Link*)malloc(sizeof(struct Link));
//printf("please input ID and score:");
scanf("%d %d",&pnode->id, &pnode->fraction);
if( list->head == NULL)
{
pnode->next = list->head; list->head = pnode;
list->last = pnode;
}else{
list->last->next = pnode;
pnode->next = NULL;
list->last = pnode;
}
}
}
void destory_Link(List *list)
{
if (list->head == NULL)
return;
struct Link *s = list->head;
struct Link *tmp = NULL;
while(s != NULL)
{
tmp = s->next;
free(s);
s = tmp;
}
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复