【C++】148 排序链表

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

对链表进行升序排序,常见的做法是使用归并排序(Merge Sort)算法。这种算法适用于链表的排序,并且具有稳定性和时间复杂度为 O(n log n) 的优点。

以下是使用 C 语言实现的归并排序算法来对链表进行排序的示例代码:

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点结构
struct ListNode {
    int val;
    struct ListNode *next;
};

// 合并两个有序链表
struct ListNode* merge(struct ListNode* l1, struct ListNode* l2) {
    if (l1 == NULL) return l2;
    if (l2 == NULL) return l1;
    
    struct ListNode dummy;
    struct ListNode* tail = &dummy;
    
    while (l1 && l2) {
        if (l1->val < l2->val) {
            tail->next = l1;
            l1 = l1->next;
        } else {
            tail->next = l2;
            l2 = l2->next;
        }
        tail = tail->next;
    }
    
    tail->next = (l1 != NULL) ? l1 : l2;
    
    return dummy.next;
}

// 归并排序
struct ListNode* sortList(struct ListNode* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    
    // 快慢指针找到中点
    struct ListNode *slow = head, *fast = head->next;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    struct ListNode* mid = slow->next;
    slow->next = NULL;
    
    // 递归排序左右两部分
    struct ListNode* left = sortList(head);
    struct ListNode* right = sortList(mid);
    
    // 合并排序后的左右两部分
    return merge(left, right);
}

// 创建新节点
struct ListNode* createNode(int val) {
    struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

// 打印链表
void printList(struct ListNode* head) {
    struct ListNode* curr = head;
    while (curr) {
        printf("%d -> ", curr->val);
        curr = curr->next;
    }
    printf("NULL\n");
}

// 释放链表内存
void freeList(struct ListNode* head) {
    struct ListNode* curr = head;
    while (curr) {
        struct ListNode* temp = curr;
        curr = curr->next;
        free(temp);
    }
}

int main() {
    // 创建示例链表:4 -> 2 -> 1 -> 3
    struct ListNode* head = createNode(4);
    head->next = createNode(2);
    head->next->next = createNode(1);
    head->next->next->next = createNode(3);
    
    printf("Original list: ");
    printList(head);
    
    // 对链表进行排序
    head = sortList(head);
    
    printf("Sorted list: ");
    printList(head);
    
    // 释放链表内存
    freeList(head);
    
    return 0;
}

使用两个辅助函数 merge 和 sortList。merge 函数用于合并两个有序链表,sortList 函数用于对链表进行归并排序

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,针对你的问题,我可以给出以下思路: 1. 定义一个链表节点结构体,包含学生姓名、学号、成绩等信息; 2. 读入学生信息,创建链表; 3. 对链表进行排序,可以使用冒泡排序、快速排序等算法; 4. 输出排序后的链表。 以下是示例代码: ```c++ #include <iostream> #include <string> using namespace std; // 定义链表节点结构体 struct Node { string name; int id; int score; Node* next; }; // 冒泡排序 void bubbleSort(Node* head) { if (head == nullptr || head->next == nullptr) { return; } bool flag = true; while (flag) { flag = false; Node* p = head->next; while (p->next != nullptr) { if (p->score < p->next->score) { swap(p->name, p->next->name); swap(p->id, p->next->id); swap(p->score, p->next->score); flag = true; } p = p->next; } } } int main() { // 读入学生信息,创建链表 Node* head = new Node(); Node* p = head; int n; cout << "请输入学生人数:"; cin >> n; for (int i = 0; i < n; i++) { Node* node = new Node(); cout << "请输入第" << i + 1 << "个学生的姓名、学号、成绩:"; cin >> node->name >> node->id >> node->score; p->next = node; p = p->next; } p->next = nullptr; // 对链表进行排序 bubbleSort(head); // 输出排序后的链表 p = head->next; while (p != nullptr) { cout << p->name << " " << p->id << " " << p->score << endl; p = p->next; } // 释放链表内存 p = head; while (p != nullptr) { Node* q = p; p = p->next; delete q; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_shsf

来包瓜子嘛,谢谢客官~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值