原题链接:[编程入门]链表合并
解题思路:先创单链表,然后本题考查的就是单链表的增删改查基本操作,最后多了排序,我们先将信息存进去,然后合并,再排序,最后输出;
注意事项:一定要抽象清晰,基础扎实
参考代码:
#include<iostream> #include<stdlib.h> using namespace std; typedef struct Node{ int student_id; int student_grade; struct Node* next; }student; student* fListCreate(){ student *p = (student*)malloc(sizeof(student)); if(p == NULL) return NULL; p->next = NULL; return p; } void fListTraverse(student *head){ student *tra; for(tra = head -> next; tra != NULL; tra = tra -> next){ cout << tra -> student_id << ' ' << tra -> student_grade << endl; } } void fListIncrease(student *ptr, int student_id, int student_grade){ student *inc = (student *)malloc(sizeof(student)); student *tra; inc -> student_id = student_id; inc -> student_grade = student_grade; for(tra = ptr; tra -> next != NULL; tra = tra -> next){ } tra -> next = inc; inc -> next = NULL; } void fListBubbleSort(student *head){ student *tra, *temp; for(tra = head; tra -> next != NULL; tra = tra -> next){ for(temp = tra -> next; temp != NULL; temp = temp -> next){ if(tra -> student_id > temp -> student_id){ int id = tra -> student_id; int grade = tra -> student_grade; tra -> student_id = temp -> student_id; tra -> student_grade = temp -> student_grade; temp -> student_id = id; temp -> student_grade = grade; } } } } int main() { int N,M; int id, grade; student *a; student *b; a = fListCreate(); b = fListCreate(); cin >> N >> M; for(int i = 0; i < N; i++){ cin >> id >> grade; fListIncrease(a, id, grade); } for(int i = 0; i < M; i++){ cin >> id >> grade; fListIncrease(b, id, grade); } for(student *tra = b -> next; tra != NULL; tra = tra -> next) { id = tra -> student_id; grade = tra -> student_grade; fListIncrease(a, id, grade); } fListBubbleSort(a); fListTraverse(a); free(a); free(b); return 0; }
0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复