简体   繁体   English

双链列表迭代器实现的问题

[英]Problems with Doubly-Linked List iterator implementation

I am having some problems with a simple Doubly-Linked list class. 我在使用简单的双向链接列表类时遇到了一些问题。

Here is all my code for it: 这是我所有的代码:

/*
 * DLList.h
 *
 *  Created on: Nov 19, 2013
 *      Author: tyler
 */

#ifndef DLLIST_H_
#define DLLIST_H_
#include <iostream>
using namespace std;

template <typename T>
class DLList{
private:
struct DLLNode{
private:
    T data_;
    bool visited_;
    DLLNode* next_;
    DLLNode* prev_;
public:

    DLLNode(T data){
        this->data_ = data;
        visited_ = false;
        next_ = NULL;
        prev_ = NULL;
    }

    void setData(T data){
        this->data_ = data;
    }

    void visit(){
        visited_ = true;
    }

    void unvisit(){
        visited_ = false;
    }

    void setNext(DLLNode* next){
        this->next_ = next;
    }

    void setPrev(DLLNode* prev){
        this->prev_ = prev;
    }

    T& getData(){
        return data_;
    }

    bool getVisited(){
        return visited_;
    }

    DLLNode* getNext(){
        return next_;
    }

    DLLNode* getPrev(){
        return prev_;
    }
};

    DLLNode* head_;
    DLLNode* tail_;

public:
    DLList(){
        head_ = NULL;
        tail_ = NULL;
    }

    class DLLiterator{
    private:
        DLLNode* node;
    public:
        DLLiterator(){
            node = head_;
        }
        T& operator*(const DLLiterator& iter){
            return iter.node->getData();
        }
        DLLiterator& operator++(const DLLiterator& iter){
            if(node->getNext() != NULL)
                node = node->getNext;
            else
                node = NULL;
            return *this;
        }
    };   

    bool isEmpty(){
        return (head_ == NULL);
    }

    void addNodeEnd(T data){
        DLLNode* temp = new DLLNode(data);

        if(isEmpty()){
            head_ = temp;
            tail_ = temp;
        }
        else{
            DLLNode* curr;
            curr = tail_;
            curr->setNext(temp);
            temp->setPrev(curr);
            tail_ = temp;
        }
    } 

    bool contains(T data){
        for(DLLNode* start = head_; start != NULL; start = start->getNext()){
            if(start->getData() == data)
                return true;
        }
        return false;
    }

    void remove(T data){
        for(DLLNode* curr = head_; curr != NULL; curr = curr->getNext()){
            DLLNode* key = curr;
            if(curr->getData() == data){
                if(curr == head_){
                    head_ = key->getNext();
                    key->getNext()->setPrev(NULL);
                    delete key;
                }
                if(curr == tail_){
                    tail_ = key->getPrev();
                    key->getPrev()->setNext(NULL);
                    delete key;
                }
                else{
                    DLLNode* prev;
                DLLNode* next;
                    prev = key->getPrev();
                    next = key->getNext();
                    prev->setNext(next);
                    next->setPrev(prev);
                    delete key;
                }
            }
        }
    }

    void printList(){
        for(DLLNode* curr = head_; curr != NULL; curr = curr->getNext()){
            cout << curr->getData() << "\n";
        }
    }
};



#endif /* DLLIST_H_ */

My problem is that I don't know how to actually use the iterator in an outside class. 我的问题是我不知道如何在外部类中实际使用迭代器。 Everything else is tested and seems to be working fine. 其他所有东西都经过测试,似乎工作正常。 Still need to add a simple destructor, but my focus right now is the iterator. 仍然需要添加一个简单的析构函数,但是我现在的焦点是迭代器。 Any help would be great. 任何帮助都会很棒。

I tried just doing: 我试着做:

DLLiterator iter;

but that is clearly wrong... 但这显然是错误的...

EDIT: This is the operator++ code now: 编辑:这是现在的operator ++代码:

iterator operator++(){
    if(node_->getNext() != NULL)
        node_ = node_->getNext;
    else
        node_ = NULL;
    return *this;
}

but now it's asking for an int as an argument...? 但是现在它要求一个int作为参数...?

Outside the class, you'd have to use the qualified name DLList<whatever>::DLLiterator . 在类之外,您必须使用限定名称DLList<whatever>::DLLiterator You can see that there's little point adding the wart to the nested name, unless you like your names to be hard to read: it just partly duplicates the scope name. 您会看到,将疣添加到嵌套名称中没有什么意义,除非您希望您的名称难以阅读:它只是部分复制了示波器名称。 I'd rename it iterator . 我将重命名为iterator

Now your problem is that it tries to initialise itself using a member of the list, head_ , but it doesn't have access to a list to extract the head from. 现在您的问题是它尝试使用列表的一个成员head_初始化自身,但是它无权访问要从中提取head的列表。 You probably want to follow the example of the standard containers, and create an iterator via a member of the list: 您可能想要遵循标准容器的示例,并通过列表的成员创建一个迭代器:

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

and modify the iterator's constructor accordingly. 并相应地修改迭代器的构造函数。

In C++11, this means users won't usually have to write out the nasty qualified name at all; 在C ++ 11中,这意味着用户通常根本不必写出讨厌的限定名称; you could get an iterator with 你可以用一个迭代器

auto it = list.begin();

Finally, remove the arguments from the iterator's operator* and operator++ . 最后,从迭代器的operator*operator++删除参数。 Since they are member functions, their operand is passed implicitly as this , not explicitly as an argument. 由于它们是成员函数,因此将其操作数作为this隐式传递,而不作为参数显式传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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