0

I have to make a project for college. I have to implement a doubly linked list on an array in C++. The thing is that I'm not allowed to use STL/templates. I implemented the DLL and I'll leave here a code sample:

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();
};

The problem is that I have to also implement an iterator and I don't really know how. I tried with nested classes but I don't know how to access the DLLA's data members from the iterator and how to generally implement it. Something like this...

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
    };
};

And also, having nested classes, is it possible to have a function getIterator() in the interface of DLLA which returns the iterator for DLLA?

Xyntell
  • 155
  • 1
  • 14
  • 4
    Read [How to implement an STL-style iterator and avoid common pitfalls?](https://stackoverflow.com/questions/8054273/how-to-implement-an-stl-style-iterator-and-avoid-common-pitfalls) – Marek Vitek Jun 29 '17 at 16:53
  • 2
    Surprisingly, my best advice here is to take a look at how iterators are implemented in the standard library (also, STL is not a term any more). – iehrlich Jun 29 '17 at 16:53

0 Answers0