C ++ - 如何为双向链表实现迭代器

时间:2017-06-29 16:49:13

标签: c++ iterator doubly-linked-list

我必须为大学做一个项目。我必须在C ++中的数组上实现双向链表。问题是我不允许使用STL /模板。我实现了DLL,我将在这里留下一个代码示例:

class DLLA {
private:
    int* elements;
    int* next;
    int* prev;
    int size;
    int capacity;
    // some other variables here if needed

public:
    DLLA();
    ~DLAA();

    int getSize();

    void addFirst(int x);
    void addLast(int x);
    void addAtPosition(int x, int position);

    // and some other operations and functions

private:
    void resize();
};

问题是我还必须实现一个迭代器,我真的不知道如何。我尝试使用嵌套类,但我不知道如何从迭代器访问DLLA的数据成员以及如何一般地实现它。像这样......

class DLLA {
public:
    // data members
private:
    // functions
public:
    class iterator {
    private:
        int current; // the current position in the vector
    public:
        void next();
        void prev();
        // and some other functions
    };
};

而且,拥有嵌套类,是否可以在DLLA的接口中有一个函数 getIterator(),它返回DLLA的迭代器?

0 个答案:

没有答案
相关问题