自定义实现 C++ list

c++的list为双向环形链表,优势在于可以快速插入删除任意节点,不能像vector通过下标快速访问元素。

#ifndef TLIST_H
#define TLIST_H
#include <stdlib.h>
#include <iterator>

using namespace std;

template <class T>
class TList
{
private:
    struct node
    {
        T date;
        node* prev;
        node* next;
    };
public:
    TList():root(nullptr),length(0)
    {
        empty_initialize();
    }

    ~TList(){}

    class iterator
    {
    public:
        node *i;

        inline iterator(): i(nullptr) {}
        inline iterator(node*n) : i(n) {}
        inline iterator(const iterator &o): i(o.i){}

        inline T &operator*() const { return i->date; }
        inline T *operator->() const { return &i->date; }
        inline bool operator==(const iterator &o) const  { return i == o.i; }
        inline bool operator!=(const iterator &o) const  { return i != o.i; }
        inline bool operator<(const iterator& other) const  { return i < other.i; }
        inline bool operator<=(const iterator& other) const  { return i <= other.i; }
        inline bool operator>(const iterator& other) const  { return i > other.i; }
        inline bool operator>=(const iterator& other) const  { return i >= other.i; }
        //++i
        inline iterator &operator++() { i = i->next; return *this; }
        //i++
        inline iterator operator++(int) {
            //node *n = i; ++i; return n;
                                          iterator tmp = *this;++(*this);return tmp;}
        //--i
        inline iterator &operator--() { i = i->prev; return *this;  }
        //i--
        inline iterator operator--(int) { iterator tmp = *this;--(*this);return tmp; }

    };

    typedef std::reverse_iterator<iterator> reverse_iterator;

    iterator begin()
    {
        return root->next;
    }
    iterator end()
    {
        return root;

    }
//    iterator rbegin()
//    {
//        return iterator(--end());
//    }
//    iterator rend()
//    {
//        return iterator(begin());

//    }

    //Test whether container is empty
    bool empty()
    {
        return (length == 0);
    }
    unsigned int size()
    {
        return length;
    }
    //Resizes the container to contain sz elements.
    void resize(unsigned int nSize,T c = T());
    //Access first element
    T& front()
    {
        return *begin();
    }
    //Access last element
    T& back()
    {
        return *(--end());
    }
    //Insert element at beginning
    void push_front ( const T& x )
    {
        insert(begin(), x);
    }

    //Delete first element
    void pop_front ()
    {
        erase(begin());
    }
    //Insert element at ending

    void push_back(const T& x )
    {
        insert(end(), x);

    }
    //Delete last element
    void pop_back ()
    {
        erase(--end());

    }
    //Insert elements
    iterator insert(iterator position, const T& x )
    {
            length++;
            node* pNode = createNode();
            pNode->date = x;
            pNode->next = position.i;
            pNode->prev = position.i->prev;
            position.i->prev->next = pNode;
            position.i->prev = pNode;
            return pNode;
    }
    //Erase elements
    iterator erase( iterator position )
    {
        if(length == 0)
            return NULL;
        length--;
        position.i->prev->next = position.i->next;
        position.i->next->prev = position.i->prev;
        iterator ret = position.i->next;
        delete position.i;
        return ret;

    }
    //Clear content
    void clear()
    {
        iterator currNode = begin();
        while ( currNode != end())
        {
            length--;
            node* temp = currNode.i;
            currNode++;
            delete temp;

        }
        root->next = root;
        root->prev = root;

    }



private:
    int length;
    node* root;

private:
    node* createNode()
    {
        node* pNode = new node();
        return pNode;

    }
    void empty_initialize()
    {
        root = createNode();
        root->next = root;
        root->prev = root;

    }

};


#endif // TLIST_H

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现在 QListview 中实时显示自定义数据,你需要做以下几个步骤: 1. 创建一个自定义的数据模型,继承自 QAbstractListModel。 2. 在模型中实现必要的函数,如 rowCount(),columnCount(),data() 等。 3. 在你的界面中创建一个 QListview 控件,并将数据模型设置为其模型。 4. 在需要更新数据的地方,调用数据模型中的数据更新函数,比如 setData()。 5. 当数据更新时,会自动触发模型的数据变更信号 dataChanged(),从而更新 QListview 中的数据。 下面是一个简单的示例代码,演示如何实现在 QListview 中实时显示自定义数据: ```c++ // 自定义数据模型 class MyModel : public QAbstractListModel { public: MyModel(QObject *parent = nullptr) : QAbstractListModel(parent) {} int rowCount(const QModelIndex &parent = QModelIndex()) const override { return m_data.size(); } QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { if (!index.isValid() || index.row() >= m_data.size()) return QVariant(); if (role == Qt::DisplayRole) return m_data.at(index.row()); return QVariant(); } bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override { if (index.isValid() && role == Qt::EditRole) { m_data.replace(index.row(), value.toString()); emit dataChanged(index, index); return true; } return false; } void addData(const QString &value) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_data.append(value); endInsertRows(); } private: QStringList m_data; }; // 在界面中使用 MyModel *model = new MyModel(this); ui->listView->setModel(model); // 更新数据 model->addData("item 1"); model->setData(model->index(0), "new item 1"); ``` 在这个示例中,我们创建了一个自定义数据模型 MyModel,重载了 rowCount(),data() 和 setData() 等函数。在界面中,我们创建了一个 QListview 控件,并将其模型设置为 MyModel。在需要更新数据时,我们调用 MyModel 中的数据更新函数,比如 addData() 和 setData(),从而实现了在 QListview 中实时显示自定义数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值