重载运算符-在内部类迭代器中的单链表

时间:2019-04-12 16:33:06

标签: c++ iterator inner-classes singly-linked-list

我需要帮助在单链接列表中声明过载的operator--。 我只需要一种获取头节点的方法或任何其他方法来重载operator--!?

即使在下面的方法中,我的错误也在于调用头部。

ERROR : invalid use of non-static data member 'list<int>::head'

我知道这是错误的,但我只想一种可行的方法。

template<class T>
struct node
{
    T data;
    node<T> *next;
};

template<class T>
class list
{
private:
    node<T> *head, *tail;
public:

    list()
    {
        head=NULL;
        tail=NULL;
    }

    list<T>& operator = (list<T> another_list){
        if( this == &another_list )
            return *this;
        iterator itr =another_list.begin( );
        this->push_back(*itr);
        do{
            ++itr;
            this->push_back(*itr);
        }while(itr != another_list.end());
        return *this;
    }

    class iterator {
    private:
        node<T> *nodeite;
    public:
        iterator(){
            nodeite=nullptr;
        }
        iterator(node<T> *Ptrnode){
            nodeite=Ptrnode;
        }

        void operator++() {
            if (nodeite->next == NULL ){
                cout << "\nOut of Range\n";
                return;
            }
            nodeite = nodeite->next;
        }

        void operator -- (){      ///TODO    /*HERE the Question */
            list<T>:: iterator i; 
            i.nodeite=head;                      ///ERROR
            if (i.nodeite == this->h ){
                cout<<"\nOut of Range\n";
            }
            else {
                while (i.nodeite->next != NULL ){
                    if(i.nodeite->next == this->nodeite){
                        this->nodeite = nodeite;
                        return;
                    }
                    ++i;
                }
            }
        }

        T& operator*() const {
            return nodeite->data;
        }

        bool operator==(const iterator& itr) const {
            return nodeite== itr.nodeite;
        }

        bool operator!=(const iterator& itr) const {
            return nodeite != itr.nodeite;
        }

    };

    iterator erase(iterator position) {
        node<T> *toDelete = position.nodeite->next;
        position.nodeite->next = position.nodeite->next->next;
        if (toDelete == tail) tail = position.nodeite;
        delete toDelete;
        return position;
    }

    void push_back(T value)
    {
        node<T> *temp=new node<T>;
        temp->data=value;
        temp->next=NULL;
        if(head==NULL)
        {
            head=temp;
            tail=temp;
            temp=NULL;
        }
        else
        {
            tail->next=temp;
            tail=temp;
        }
    }

    void insert(int pos, T value)
    {
        node<T> *pre=new node<T>;
        node<T> *cur=new node<T>;
        node<T> *temp=new node<T>;
        cur=head;
        for(int i=1; i<pos; i++)
        {
            pre=cur;
            cur=cur->next;
        }
        temp->data=value;
        pre->next=temp;
        temp->next=cur;
    }



    iterator begin() {
      return iterator(head);
    }

    iterator end() {
      return iterator(tail);
    }

};

0 个答案:

没有答案