0

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

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

#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 

当然有一种方法可以通过列表仅进行一次迭代进行排序?或者可能不是。

提前感谢您的帮助!

4

1 回答 1

1

可以使用基数排序,并且有一篇关于链表排序的非常好的文章,比在 O(n * n) 时间内执行一个节点与每个其他节点的比较更有效

于 2013-04-03T11:40:38.183 回答