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

时间:2014-04-22 06:56:04

标签: c++ data-structures

我正在使用这本教科书

http://cpp.datastructures.net

第5章迭代器用法:

http://cpp.datastructures.net/source/ch05/CPP/IteratorPrint.cpp-DSACiterator.html

这是我如何实现它(ObjectIterator类)

#include <iostream>

using namespace std;

class PositionException {
protected:
    string message;
public:
    PositionException(const string &m) {
        message = m;
    }
    void print() {
        cout << message << endl;
    }
};


template <typename T>

class NodeList {

protected:

    struct Node {
        Node *prev;
        Node *next;
        T item;
        Node(T i = T(), Node *p = NULL, Node *n = NULL) : item(i), prev(p), next(n) {}

    };


    typedef Node * NodePtr;

public:
    class Position {
    protected:
        NodePtr node;
    public:
        bool isNull() {
            return node == NULL;
        }
        Position(NodePtr n = NULL) : node(n) {}
        T& element() {
            return node->item;
        }

        friend class NodeList;
    };


    class ObjectIterator {
    protected:
        NodePtr node;
    public:
        ObjectIterator(NodePtr n = NULL) : node(n) {}

        bool hasNext() {

            if (node->next == NULL) {
                return false;
            }

            if (node->next->next == NULL) {
                return false;
            }
            return true;
        }
        ObjectIterator next() {
            return ObjectIterator(node->next);
        }
        T& element() {
            return node->item;
        }
        friend class NodeList;


    };

protected:
    int sz;
    NodePtr header;
    NodePtr trailer;

public:
    NodeList() {
        header = new Node();
        trailer = new Node();
        header->next = trailer;
        trailer->prev = header;
        sz = 0;
    }

    bool isEmpty() const {
        return size() == 0;
    }
    int size() const {
        return sz;
    }

    bool isFirst(const Position& p) const {
        return p.node->prev == header;
    }
    bool isLast(const Position& p) const {
        return p.node->next = trailer;
    }


    Position first() const {
        if (isEmpty()) {
            throw PositionException("no first for empty list");
        }
        return Position(header->next);
    }

    Position last() const {
        if (isEmpty()) {
            throw PositionException("no last for emtpy list");
        }
        return Position(trailer->prev);
    }


    Position before(const Position& p) const{
        if (p.node->prev == header) {
            throw PositionException("already the first element, nothing before it");
        }
        return Position(p.node->prev);
    }

    Position after(const Position& p) const{
        if (p.node->next == trailer) {
            throw PositionException("already the last element, nothing after it");
        }
        return Position(p.node->next);
    }




    Position insertAfter(const Position& p, const T& o) {
        NodePtr node = new Node(o, p.node, p.node->next);
        p.node->next->prev = node;
        p.node->next = node;
        sz++;
        return Position(node);
    }

    Position insertBefore(const Position& p, const T& o) {
        NodePtr node = new Node(o, p.node->prev, p.node);
        p.node->prev->next = node;
        p.node->prev = node;
        sz++;
        return Position(node);
    }

    void remove(const Position& p) {
        p.node->prev->next = p.node->next;
        p.node->next->prev = p.node->prev;

        sz--;
        delete p.node;
    }

    void removeFirst() {
        remove(first());
    }

    void removeLast() {
        remove(last());
    }


    void replaceElement(const Position& p, const T& element) {
        if (p.isNull()) {
            throw PositionException("p is null");
        }
        p.node->item = element;
    }

    Position insertFirst(const T& o) {
        NodePtr node = new Node(o, header, header->next);
        header->next->prev = node;
        header->next = node;
        sz++;
        return Position(node);
    }

    Position insertLast(const T& o) {
        NodePtr node = new Node(o, trailer->prev, trailer);
        trailer->prev->next = node;
        trailer->prev = node;
        sz++;
        return Position(node);
    }

    void copyFrom(const NodeList<T>& nl) {
        sz = nl.sz;

        if (nl.sz > 0) {
            Position p0 = nl.first();
            Position p = insertFirst(p0.node->item);

            while (!nl.isLast(p0)) {
                p0 = nl.after(p0);
                insertAfter(p, p0.node->item);
            }
        }
    }

    void emptyList() {
        while (!isEmpty()) {
            removeFirst();
        }
    }

    ~NodeList() {
        emptyList();
        delete header;
        delete trailer;
    }

    NodeList<T>& operator=(const NodeList<T>& nl) {
        emptyList();
        copyFrom(nl);
    }

    NodeList(const NodeList<T>& nl) {
        emptyList();
        copyFrom(nl);
    }

    void print() {
        cout << "size is: " << size() << endl;

        if (size() > 0) {
            ObjectIterator i = elements();
            while (i.hasNext()) {
                cout << i.element() << "\t";
                i = i.next();
            }
            cout << endl;
        }
    }


    ObjectIterator elements() {
        if (isEmpty()) {
            throw PositionException("iterator error: empty");
        }
        return ObjectIterator(header->next);
    }

    void swapItems(const Position& p1, const Position& p2) {
        T temp = p1.node->item;
        p1.node->item = p2.node->item;
        p2.node->item = temp;

    }

};

如果我不使用迭代器,下面的打印代码是正确的

void print() {
    if (size() > 0) {

        NodePtr n = header->next;
        while (n != trailer) {
            cout << n->item << "\t";
            n = n->next;
        }

        cout << endl;
    }

}

如果我使用迭代器作为打印功能

void print() {
    cout << "size is: " << size() << endl;

    if (size() > 0) {
        ObjectIterator i = elements();
        while (i.hasNext()) {
            cout << i.element() << "\t";
            i = i.next();
        }
        cout << endl;
    }
}

其中一个节点丢失了。 本书没有提供实现迭代器的正确方法

1 个答案:

答案 0 :(得分:2)

你的hasNext()函数对于双向链表是正确的,假设预告片的下一个是NULL。 错误是打印功能中的用法

void print() {
    cout << "size is: " << size() << endl;

    if (size() > 0) {
        ObjectIterator i = elements();

        while (true) {
            cout << i.element() << "\t";
            if (i.hasNext())
                i = i.next();
            else
                break;

        }
        cout << endl;
    }
}

编辑: 上面的代码只为你做调试。这不是你如何实现'迭代器'

首先,改变next()实现:

    ObjectIterator next() {
        return (*this = ObjectIterator(node->next));
    }

现在,不是返回第一个元素,而是返回标题以进行迭代

ObjectIterator elements() {
    return ObjectIterator(header);
}

现在你可以使用类似于书和STL的语法:

void print() {
    cout << "size is: " << size() << endl;

    ObjectIterator i = elements();

    while (i.hasNext()) {
        cout << i.next().element() << "\t";
    }
    cout << endl;
}

请注意,由于您返回标题,因此无需检查(size()&gt; 0)

编辑2: 在next()中,您可以推进迭代器的节点而不是迭代器容器本身

    ObjectIterator next() {
        node = node->next;
        return *this;
    }