解题思路:先创单链表,然后本题考查的就是单链表的增删改查基本操作,最后多了排序,我们先将信息存进去,然后合并,再排序,最后输出;
注意事项:一定要抽象清晰,基础扎实
参考代码:
#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语言考试练习题_保留字母 (C语言代码)浏览:616 |
点我有惊喜!你懂得!浏览:4112 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:559 |
C语言程序设计教程(第三版)课后习题8.9 (C语言代码)浏览:690 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:1215 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:583 |
求圆的面积 (C语言代码)浏览:1756 |
字符逆序 (C语言代码)浏览:506 |
简单的a+b (C语言代码)浏览:574 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:755 |