自定义链接列表排序方法

时间:2013-04-03 07:34:26

标签: c++ sorting linked-list

您好我正在制作自定义链接列表(为了好玩!)并希望实现一种排序方法,使用存储的类型对存储的数据进行排序>和<运算符(将要求类型使这些运算符重载)

最好的方法是什么?我想可能跳到的第一件事就是将当前节点与节点比较为“右”,看看是否有一个比另一个更大。然后继续遍历列表,直到您不再交换任何节点。但我认为可能有更有效的方法来做到这一点。到目前为止,这是我的代码:

#ifndef LIST_HPP
#define LIST_HPP

#include <string>

template <typename T>
class Node;

template <typename T>
class List
{   
    public:
        List();
        ~List();

        bool isEmpty() const;
        T& front();
        T& back();
        inline unsigned int size() const;
        void pushFront(const T& s);
        void removeElement(const T& s);
        void insert(const T& s);
        T popFront();
        const T* each();

    private:
        unsigned int size_;
        Node<T>* head_;
        Node<T>* current_;      
};

template <typename T> List<T>::List() : size_(0), head_(0), current_(0)
{
}

template <typename T> List<T>::~List()
{
    while (isEmpty())
    {
        popFront();
    }
    head_ = 0;
}

template <typename T> bool List<T>::isEmpty() const
{
    return (!size_ || !head_);
}


template <typename T> T& List<T>::front()
{
    if (isEmpty())
        throw std::string("List Empty");

    return head_->data();
}

template <typename T> T& List<T>::back()
{
    Node<T>* cur = head_;

    while (true)
    {
        if (cur->next() == 0)
            return cur->data();

        cur = cur->next();
    }
}

template <typename T> void List<T>::pushFront(const T& s)
{
    current_ = head_ = new Node<T>(s, head_);

    size_++;
}

template <typename T> T List<T>::popFront()
{
    if (isEmpty())
    {
        throw std::string("List Empty");
    }

    Node<T>* current_head = head_;
    current_ = head_ = head_->next();

    T temp = current_head->data();

    delete current_head;

    size_--;

    return temp;
}
template <typename T> const T* List<T>::each()
{
    if (isEmpty())
        return 0;

    if (current_ == 0)
    {
        current_ = head_;
        return 0;
    }

    const T* ret_str = &current_->data();

    current_ = current_->next();

    return ret_str;
}
template <typename T> void List<T>::removeElement(const T& s)
{
    if (isEmpty())
    {
        throw std::string("List Empty");
        return;
    }

    Node<T>* prev = 0, *rnode;

    rnode = head_;

    while (rnode != 0)
    {
        if (rnode->data() == s)
            break;

        prev = rnode;
        rnode = rnode->next();
    }

    if (prev == 0)
    {
        Node<T>* temp = head_;
        current_ =  head_ = head_->next();

        delete temp;        
    }
    else
    {
        prev->next(rnode->next());
        delete rnode;
    }

    size_--;
}
template <typename T> void List<T>::insert(const T& s)
{
    if (isEmpty())
    {
        pushFront(s);
    }
    else if (size_ == 1)
    {
        if (s <= head_->data())
            pushFront(s);
        else
            head_->next(new Node<T>(s, 0));

        size_++;
    }
    else
    {
        Node<T>* current, *prev = 0, *new_node = 0;

        while (current != 0)
        {
            if (s <= current->data())
                break;

            prev = current;
            current = current->next();
        }

        if (prev == 0)
            pushFront(s);
        else
            prev->next(new Node<T>(s, current));

        size_++;

    }

}

template <typename T> unsigned int List<T>::size() const {return size_;}

template <typename T>
class Node
{
    public:
        Node(const T& data, Node<T>* next = 0);
        T& data();
        Node* next();
        void next(Node<T>* n);

    private:
        T data_;
        Node* next_;
};

template <typename T> Node<T>::Node(const T& data, Node* next): data_(data), next_(next)
{
}

template <typename T> void Node<T>::next(Node<T>* n)
{
    next_ = n;
}
template <typename T> Node<T>* Node<T>::next()
{
    return next_;
}
template <typename T> T& Node<T>::data()
{
    return data_;
}
#endif //LIST_HPP 

当然有一种方法可以在列表中只进行一次迭代排序吗?或许不是。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

可以使用Radix Sort并且有一篇关于链表排序的非常好的文章比在O(n * n)时间here中对每个其他节点比较执行节点更有效。