C++单链表实现冒泡排序

来源:互联网 发布:mac刷新桌面 编辑:程序博客网 时间:2024/05/16 07:06

好学的丽华学妹问了我一个冒泡排序的问题,由于没有用数组,她瞬间就不会写了。好吧,其实之前本人也没有写过,但是对于冒泡排序还是比较熟悉的,就试着用其中的思想对照着写了一个小demo,当然,写得匆忙,很不规范滴就用了一个文件就暴力滴搞定了。如果有错误,希望您批评指正呀。全部代码如下:


#include <iostream>using namespace std;struct node{    int index;    node* next;};class MyList{private:    node* head;    int length;public:    MyList()    {        head = NULL;//头指针为空        length = 0;//长度为0    }    ~MyList()    {        node* left = head;        node* right = NULL;        while (left != NULL)        {            right = left;            left = left->next;            delete right;        }    }    void addNode(int user_index)    {        if (isEmpty())        {            head = new node();            head->next = NULL;            head->index = user_index;        }        else        {            //创建一个新的节点            node* newnode = new node();            newnode->index = user_index;            newnode->next = NULL;            //将节点添加到链表的最末端            node* t = head;            while (t->next!=NULL)            {                t = t->next;            }            t->next = newnode;            length++;        }    }    int getLength()    {        return length;    }    void display()    {        if (isEmpty())        {            cout << "The list is empty!";            return;        }        node* temp = head;        while (temp)        {            cout << temp->index;            if (!temp->next)//已至链表尾,可以结束输出了。            {                break;            }            cout << "->";            temp = temp->next;        }        cout << endl;    }    void lhInput()    {        for (int i = 12; i>0; i--)        {            addNode(i);        }    }    bool isEmpty()    {        if (head==NULL)        {            return true;        }        else        {            return false;        }    }    void bubbleSort()    {        if (isEmpty())        {            return;        }        //temp指针用来进行指向要交换的两个节点的左边一个        node* temp = head;        while (temp && temp->next)        {            if (temp->index > temp->next->index)            {                exchangeNode(temp, temp->next);            }        }        //尾指针总是指向已经排好的元素的首地址,这里我们先移到链表尾部等待        node* tail = head;        while (tail->next)        {            tail = tail->next;        }        //外层还是数组的思想,内层就是链表的思想了,为什么外层要用数组的思想呢?因为这样比较简洁,不易搞错        for (int i = 0; i < length; i++)        {            temp = head;            while (temp->next != tail)            {                if (temp->index > temp->next->index)                {                    exchangeNode(temp, temp->next);                }            }            tail = temp;        }    }    //交换相邻两个节点    void exchangeNode(node* left, node* right)    {        //如果left是头结点        if (left==head)        {            left->next = right->next;            right->next = left;            head = right;            return;        }        //找到左节点的前一个节点        node* before_left = head;        while (before_left->next!=left)        {            before_left = before_left->next;        }        before_left->next = right;        left->next = right->next;        right->next = left;    }};int main(){    MyList hengbao;    hengbao.lhInput();    hengbao.display();    hengbao.bubbleSort();    hengbao.display();    system("pause");    return 0;}

其实,如果是要实现排序,用指针实现快排可能是个不错的选择,当然本人还不能顺利写出来……其实运用插入排序解决是不是会简单一些


  1. 干掉一个节点,记录这个节点的信息
  2. 创建一个新的节点,赋值为刚刚被干掉的节点的信息
  3. 插入有序的链表中

没实现这种思路,有兴趣的小盆友可以试一试。另外,欢迎拍砖!

0 0
原创粉丝点击