【数据结构与算法】有n个记录存储在带头结点的双向链表中,现用双向冒泡排序法对其按上升序进行排序

题目

  Qestion:有n个记录存储在带头结点的双向链表中,现用双向冒泡排序法对其按上升序进行排序,请写出这种排序的算法。

  注: 双向冒泡排序即相邻两趟排序向相反方向冒泡。


主要思路

利用双链表的特性,将原本一次循环进行一次上升或者下降的冒泡排序进行拓展,变成为一次循环下降一次(大的沉底)上升一次(小的上浮),因此一次循环最好可以向有序迈前进“两步“并非”一步“


数据结构与定义:

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

// 链表节点结构
struct Node
{
    int data;          // 数据
    struct Node *prev; // 前驱
    struct Node *next; // 后继
};

核心函数

// 双向冒泡排序
void bilateralBubbleSort(struct Node *head)
{
    int swapped; // 标记是否发生交换
    struct Node *start = head;
    struct Node *end = NULL;

    // 检查链表是否为空
    if (head == NULL)
        return;

    do
    {
        swapped = 0;
        struct Node *current = start; // 从头开始冒泡

        // 从左往右冒泡,将较大的节点冒泡到右侧或者最后一个节点
        while (current->next != end)
        {
            if (current->data > current->next->data)// 如果当前节点的数据大于下一个节点的数据则交换
            {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;// 指针后移
        }
        end = current; // 将最后一个节点赋值给end

        if (!swapped)// 如果没有发生交换则说明已经排好序了,则退出循环
            break;

        swapped = 0; // 前面有交换的话swapped为1,这里需要重置swapped
        current = end; // 从最后一个节点开始冒泡

        // 从右往左冒泡,将较小的节点冒泡到左侧
        while (current->prev != start)
        {
            if (current->data < current->prev->data)
            {
                swapData(current, current->prev);
                swapped = 1;
            }
            current = current->prev;
        }
        start = current;// 将第一个节点赋值给start
        // 重复以上步骤,直到没有发生交换
    } while (swapped);
}

主要操作函数

// 创建新节点
struct Node *createNode(int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode; // 返回新节点
}

// 在链表末尾添加节点
void appendNode(struct Node **head, int data) // 传入指针的指针
{
    struct Node *newNode = createNode(data); // 创建新节点
    if (*head == NULL)
    {
        *head = newNode; // 如果链表为空, 则新节点为头节点
        return;
    }
    struct Node *temp = *head; // 临时指针指向头节点
    while (temp->next != NULL)
    {
        temp = temp->next; // 遍历链表, 直到最后一个节点
    }
    temp->next = newNode; // 最后一个节点的后继指向新节点
    newNode->prev = temp; // 新节点的前驱指向最后一个节点
}

// 交换节点数据
void swapData(struct Node *a, struct Node *b) // 传入的参数a和b是指针
{
    int temp = a->data; // 交换a和b的数据
    a->data = b->data;
    b->data = temp;
}

完整代码

/*
 * @Author: hiddenSharp429 z404878860@163.com
 * @Date: 2023-06-12 16:10:38
 * @LastEditors: hiddenSharp429 z404878860@163.com
 * @LastEditTime: 2023-06-12 16:55:27
 * @FilePath: \appe:\C OR C++\code\HW15.2.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <stdlib.h>

// 链表节点结构
struct Node
{
    int data;          // 数据
    struct Node *prev; // 前驱
    struct Node *next; // 后继
};

// 创建新节点
struct Node *createNode(int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode; // 返回新节点
}

// 在链表末尾添加节点
void appendNode(struct Node **head, int data) // 传入指针的指针
{
    struct Node *newNode = createNode(data); // 创建新节点
    if (*head == NULL)
    {
        *head = newNode; // 如果链表为空, 则新节点为头节点
        return;
    }
    struct Node *temp = *head; // 临时指针指向头节点
    while (temp->next != NULL)
    {
        temp = temp->next; // 遍历链表, 直到最后一个节点
    }
    temp->next = newNode; // 最后一个节点的后继指向新节点
    newNode->prev = temp; // 新节点的前驱指向最后一个节点
}

// 交换节点数据
void swapData(struct Node *a, struct Node *b) // 传入的参数a和b是指针
{
    int temp = a->data; // 交换a和b的数据
    a->data = b->data;
    b->data = temp;
}

// 双向冒泡排序
void bilateralBubbleSort(struct Node *head)
{
    int swapped; // 标记是否发生交换
    struct Node *start = head;
    struct Node *end = NULL;

    // 检查链表是否为空
    if (head == NULL)
        return;

    do
    {
        swapped = 0;
        struct Node *current = start; // 从头开始冒泡

        // 从左往右冒泡,将较大的节点冒泡到右侧或者最后一个节点
        while (current->next != end)
        {
            if (current->data > current->next->data)// 如果当前节点的数据大于下一个节点的数据则交换
            {
                swapData(current, current->next);
                swapped = 1;
            }
            current = current->next;// 指针后移
        }
        end = current; // 将最后一个节点赋值给end

        if (!swapped)// 如果没有发生交换则说明已经排好序了,则退出循环
            break;

        swapped = 0; // 前面有交换的话swapped为1,这里需要重置swapped
        current = end; // 从最后一个节点开始冒泡

        // 从右往左冒泡,将较小的节点冒泡到左侧
        while (current->prev != start)
        {
            if (current->data < current->prev->data)
            {
                swapData(current, current->prev);
                swapped = 1;
            }
            current = current->prev;
        }
        start = current;// 将第一个节点赋值给start
        // 重复以上步骤,直到没有发生交换
    } while (swapped);
}

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

// 主函数
int main()
{
    struct Node *head = NULL;
    int n, data;

    printf("输入链表中记录的个数: ");
    scanf("%d", &n);

    printf("输入链表中的记录: ");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &data);
        appendNode(&head, data);
    }

    printf("排序前的链表: ");
    printList(head);

    bilateralBubbleSort(head);

    printf("排序后的链表: ");
    printList(head);

    return 0;
}


结束语

  因为是算法小菜,所以提供的方法和思路可能不是很好,请多多包涵~如果有疑问欢迎大家留言讨论,你如果觉得这篇文章对你有帮助可以给我一个免费的赞吗?我们之间的交流是我最大的动力!

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hiddenSharp429

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值