数据结构(C/C++)双向链表双向冒泡排序

数据结构(C/C++)

P271 3.2

  • 用双向链表做冒泡排序
/*
    有n个记录在带头节点双向链表中,用双向冒泡排序成 升序
*/
#include <iostream>
using namespace std;

struct numNode
{

    int data;
    numNode *next;
    numNode *before;
};

class bubleLink
{
private:
    numNode *head;
    numNode *rear;
    bool sorted = false;
    numNode *logicalHead, *logicalRear;

public:
    bubleLink(int n, int *num)
    {
        /*
            初始化一个链表数

            *@params: n  int: 总共个数
            *@params: *i int: 输入的数字数组
            return:
        */

        // 初始化不带数据的头节点
        head = (numNode *)malloc(sizeof(numNode));
        head->next = NULL;
        head->data = INT_MIN;
        head->before = NULL;
        logicalHead = head;
        // 创建一个临时存放当前节点的变量
        numNode *curreentNode = head;

        for (int i = 0; i < n; i++)
        {
            //创建下一个节点
            curreentNode->next = (numNode *)malloc(sizeof(numNode));
            curreentNode->next->data = num[i];
            curreentNode->next->next = NULL;
            curreentNode->next->before = curreentNode;
            // 当前节点滑动至下一个节点
            curreentNode = curreentNode->next;
        }
    }
    void exchange(numNode *node_one, numNode *node_two)
    {
        /*
            交换两个节点的值

            *@params: *node_one  numNode: 节点1
            *@params: *node_two  numNode: 节点2
            return:
        */
        int temp = node_one->data;
        node_one->data = node_two->data;
        node_two->data = temp;
    }
    // void sortFromHead(numNode *logicalHead, numNode *logicalRear)
    numNode *sortFromHead(numNode *logicalHead, numNode *logicalRear)
    {
        /*
            从头开始升序冒泡

            *@params: *logicalHead  numNode: 已排序的头部
            *@params: *logicalRear  numNode: 已排序的尾部
            return: 
        */
        numNode *temp = logicalHead;
        sorted = true;
        while (temp->next != NULL)
        {
            if (temp->data > temp->next->data)
            {
                exchange(temp, temp->next);
                sorted = false;
            }
            temp = temp->next;
        }
        return temp;
        // this.logicalRear = temp;
    }
    // void sortFromRear(numNode *logicalHead, numNode *logicalRear)
    numNode *sortFromRear(numNode *logicalHead, numNode *logicalRear)
    {
        /*
            从尾开始升序冒泡

            *@params: *logicalHead  numNode: 已排序的头部
            *@params: *logicalRear  numNode: 已排序的尾部
            return: 
        */
        sorted = true;
        numNode *temp = logicalRear;
        while (temp->before != NULL)
        {
            if (temp->data < temp->before->data)
            {
                exchange(temp, temp->before);
                sorted = false;
            }
            temp = temp->before;
        }
        return temp;
        // this.logicalHead = temp;
    }
    void sort()
    {
        // 升序排序
        while (!sorted)
        {
            logicalRear = sortFromHead(logicalHead, logicalRear); // 从头部开始排序
            logicalHead = sortFromRear(logicalHead, logicalRear); // 从尾部开始排序
        }
    }
    void printLink()
    {
        // 打印链表
        numNode *temp = head;
        while (temp->next != NULL)
        {
            cout << temp->next->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
};

int main()
{
    int n;
    cin >> n;
    int num[n];
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
    }
    bubleLink bu(n, num);
    bu.sort();
    cout << "sorted -> ";
    bu.printLink();
    return 0;
}

运行结果例子:

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
数据结构课程设计中,C++双向链表是一个常见的数据结构之一。它是一种线性数据结构,由多个节点组成,每个节点包含两个指针,一个指向前一个节点,一个指向后一个节点。双向链表相比于单向链表,可以实现双向遍历。 在C++中,可以通过定义一个双向链表类来实现双向链表的功能。以下是一个简单的C++双向链表的实现示例: ```cpp #include <iostream> // 双向链表节点定义 class Node { public: int data; Node* prev; Node* next; }; // 双向链表类定义 class DoublyLinkedList { private: Node* head; // 头节点指针 public: DoublyLinkedList() { head = nullptr; } // 在链表头部插入节点 void insertAtHead(int value) { Node* newNode = new Node(); newNode->data = value; newNode->prev = nullptr; newNode->next = head; if (head != nullptr) { head->prev = newNode; } head = newNode; } // 在链表尾部插入节点 void insertAtTail(int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = nullptr; if (head == nullptr) { newNode->prev = nullptr; head = newNode; return; } Node* temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; newNode->prev = temp; } // 打印链表元素 void printList() { Node* temp = head; while (temp != nullptr) { std::cout << temp->data << " "; temp = temp->next; } std::cout << std::endl; } }; int main() { DoublyLinkedList dll; dll.insertAtHead(3); dll.insertAtHead(2); dll.insertAtHead(1); dll.printList(); // 输出:1 2 3 dll.insertAtTail(4); dll.insertAtTail(5); dll.printList(); // 输出:1 2 3 4 5 return 0; } ``` 以上是一个简单的C++双向链表的实现示例。你可以通过调用`insertAtHead`和`insertAtTail`方法来插入节点,通过调用`printList`方法来打印链表元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

左手八嘎呀路

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

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

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

打赏作者

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

抵扣说明:

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

余额充值