迭代类型结构的链接LIst

时间:2012-11-26 05:10:42

标签: c++ struct iterator linked-list

我有一个结构的链表:

typedef struct {
    std::string title;
    int rating;
} listing;

和标准迭代器是我列表的一部分:

template<class T>
class ListIterator
{
public:
    ListIterator( ) : current(NULL) {}

    ListIterator(Node<T>* initial) : current(initial) {}

    const T& operator *( ) const { return current->getData( ); }
    //Precondition: Not equal to the default constructor object,
    //that is, current != NULL.

    ListIterator& operator ++( ) //Prefix form
    {
        current = current->getLink( );
        return *this;
    }

    ListIterator operator ++(int) //Postfix form
    {
        ListIterator startVersion(current);
        current = current->getLink( );
        return startVersion;
    }

    bool operator ==(const ListIterator& rightSide) const
    { return (current == rightSide.current); }

    bool operator !=(const ListIterator& rightSide) const
    { return (current != rightSide.current); }

    //The default assignment operator and copy constructor
     //should work correctly for ListIterator,
private:
    Node<T> *current;
};

这在处理标准数据类型列表(int,char,string等)时工作正常,但我无法弄清楚如何遍历列表中每个结构中的数据。

这可能是由于我如何存储它们:

// declaration of list, Queue is list class name
Queue<listing> list;

// data reads from file
while (!datafile.eof()) {

    listing entry = *new listing;

    datafile >> listing.rating;
    std::getline(datafile, listing.title);

    list.add(entry);

}

希望这不是太模糊。如果您需要查看更多代码,请告诉我们。

节点和队列类:

template<class T>
class Node
{
public:
    Node(T theData, Node<T>* theLink) : data(theData), link(theLink){}
    Node<T>* getLink( ) const { return link; }

    const T& getData( ) const { return data; }

    void setData(const T& theData) { data = theData; }
    void setLink(Node<T>* pointer) { link = pointer; }

private:
    T data;
    Node<T> *link;
};

template<class T>
class Queue
{
public:
    typedef ListIterator<T> Iterator;

    Queue( );
    // Initializes the object to an empty queue.

    Queue(const Queue<T>& aQueue);

    Queue<T>& operator =(const Queue<T>& rightSide);

    virtual ~Queue( );

    void add(T item);
    // Postcondition: item has been added to the back of the queue.

    T remove( );
    // Precondition: The queue is not empty.
    // Returns the item at the front of the queue
    // and removes that item from the queue.

    bool isEmpty( ) const;
    // Returns true if the queue is empty. Returns false otherwise.

    Iterator begin() const { return Iterator(front); }
    Iterator end() const { return Iterator(); }

private:
    Node<T> *front; // Points to the head of a linked list. 
                    // Items are removed at the head

    Node<T> *back; // Points to the node at the other end of the linked list.
                   // Items are added at this end.
};

1 个答案:

答案 0 :(得分:0)

解决方案非常简单。我很尴尬,我之前没有看到它。

Queue<listing>::Iterator all;
for (all = list.begin(); all != list.end(); all++) {

    listing entry = *all;
    cout << entry.title << " ";
    cout << entry.rating << "\n";

}
相关问题