简体   繁体   中英

Implement iterator of a circular list

I'm trying to implement a class of Circular List with a nested class of iterator and I wrote like this:

template <class T>
class CircularList {
    struct Item {
        T data;
        Item* next;
    };
    Item* head;
    int size;
public:
    CircularList() {
        head = new Item();
        head->next = head;
    }
    int sizeList() { return size; }
    void push(T data) {
        Item* i = new Item();
        i->data = data;
        i->next = head->next;
        head->next = i;
        size++;
    }
    class CircularListIterator {
        Item* p;
        CircularListIterator() {
            p = head->next;
        }
        bool hasNext() {
            if(p->next != head) {
                return true;
            }
            return false;
        }
        T next() {
            T data_temp = p->data;
            p = p->next;
            return data_temp;
        }
};
            CircularListIterator* iterator() {
                return new CircularListIterator();
            }
    }; 

int main() {
    CircularList<string>* letters = new CircularList<string>;
    letters->push("d");
    letters->push("c");
    letters->push("b");
    letters->push("a");

    Iterator<string>* it= new Iterator<string>;
    it = letters->iterator();
    while (it->hasNext()) {
        cout<< it->next() << "," << endl;
    }
    return 0;
}

But the Iterator is not working when I try to create an iterator in the main function, It said that it wasn't declared in the scope and has no member of it.

Assuming by "in the main class" you mean in the main function , the problem is quite straightforward: you're trying to construct a ::Iterator<string> , but there is no class in the global namespace (or anywhere else, in this code sample) called Iterator ! You could try constructing a CircularList<string>::CircularListIterator - that's at least a class that exists - but it wouldn't work because the iterator needs to be associated with a CircularList object for it to be able to access member variables like head .

The correct thing to do here is to promote the iterator function - the one that returns a CircularListIterator* - out of the CircularListIterator class and into the CircularList class. Then, in your main function, you can call letters->iterator() and it'll return a CircularListIterator* for the letters object.

Now, CircularListIterator doesn't inherit from any other iterator classes - neither the (nonexistent-in-this-code Iterator you've typed it as, nor the C++ std::iterator or any of its variants) - so you can't assign it to it or probably even compile the code that references Iterator . To make CircularListIterator a subclass of std::iterator , you'll need to extend std::iterator<Category, T> with the appropriate category. See https://www.cplusplus.com/reference/iterator/iterator/ for more information on the std::iterator class template, including an example of implementing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM