c++运用链表将学生成绩从高到低排序运用sort函数进行排序
#include <iostream>
#include <algorithm>
using namespace std;
// 学生结构体
struct Student {
string name;
int score;
};
// 链表节点结构体
struct Node {
Student data;
Node* next;
};
// 链表类
class LinkedList {
public:
Node* head;
// 链表初始化
LinkedList() {
head = nullptr;
}
// 向链表尾部添加节点
void addNode(Student student) {
Node* newNode = new Node();
newNode->data = student;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
return;
}
Node* cur = head;
while (cur->next != nullptr) {
cur = cur->next;
}
cur->next = newNode;
}
};
// 对链表排序
void sortLinkedList(LinkedList* list) {
// 存放链表节点指针的vector
vector<Node*> nodes;
Node* cur = list->head;
while (cur != nullptr) {
nodes.push_back(cur);
cur = cur->next;
}
// 使用sort算法对vector排序
sort(nodes.begin(), nodes.end(), [](Node* a, Node* b) {
return a->data.score > b->data.score;
});
// 重新连接链表节点
list->head = nodes[0];
for (int i = 0; i < nodes.size() - 1; i++) {
nodes[i]->next = nodes[i+1];
}
nodes[nodes.size()-1]->next = nullptr;
}
int main() {
LinkedList list;
list.addNode({ "张三", 98 });
list.addNode({ "李四", 95 });
list.addNode({ "王五", 92 });
sortLinkedList(&list);
Node* cur = list.head;
while (cur != nullptr) {
cout << cur->data.name << " " << cur->data.score << endl;
cur = cur->next;
}
}
我需要在c++中利用链表对学生成绩进行排序。请问如何使用sort函数对链表进行从高到低的排序?请提供一些代码示例以便理解实现方法。
对于链表的排序,需要先将链表转成vector,然后对vector进行排序,最后再将排序后的结果重新转回链表。具体实现如下:
假设链表节点定义如下:
struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} };
首先将链表转存到vector中:
vector vec; ListNode *p = head; while (p != nullptr) { vec.push_back(p->val); p = p->next; }
然后使用sort函数进行排序:
sort(vec.begin(), vec.end(), greater());
最后将排序后的vector重新转回链表:
ListNode sorted_head = nullptr; ListNode tail = nullptr; for (auto val : vec) { ListNode* node = new ListNode(val); if (!sorted_head) { sorted_head = tail = node; } else { tail->next = node; tail = tail->next; } }
完整代码如下:
ListNode sortList(ListNode head) { if (!head || !head->next) return head; vector vec; ListNode p = head; while (p != nullptr) { vec.push_back(p->val); p = p->next; } sort(vec.begin(), vec.end(), greater()); ListNode sorted_head = nullptr; ListNode tail = nullptr; for (auto val : vec) { ListNode node = new ListNode(val); if (!sorted_head) { sorted_head = tail = node; } else { tail->next = node; tail = tail->next; } } return sorted_head; }
#include <iostream>
using namespace std;
struct Student{
string name;
int score;
};
struct ListNode {
Student stu;
ListNode * next;
};
bool cmp(Student a, Student b){
return a.score > b.score; // 按照分数从高到低排序
}
void sortList(ListNode * head){
vector<Student> arr; // 将链表数据存入数组中
while(head != NULL){
arr.push_back(head->stu);
head = head->next;
}
sort(arr.begin(), arr.end(), cmp); // 使用sort函数排序
ListNode * ptr = head;
int i = 0;
while(ptr != NULL){ // 将排序后的数组中的数据赋回链表
ptr->stu = arr[i++];
ptr = ptr->next;
}
}
int main(){
ListNode * head = new ListNode; // 假设链表中有多个学生,这里以三个为例
head->stu.name = "Tom";
head->stu.score = 90;
head->next = new ListNode;
head->next->stu.name = "Jerry";
head->next->stu.score = 85;
head->next->next = new ListNode;
head->next->next->stu.name = "Bob";
head->next->next->stu.score = 95;
head->next->next->next = NULL;
sortList(head); // 进行排序
while(head != NULL){ // 遍历链表输出排序后的结果
cout << head->stu.name << ": " << head->stu.score << endl;
head = head->next;
}
return 0;
}