2012-11-26 52 views
0

我有一個結構的鏈接列表:迭代結構類型的鏈表

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

爲什麼'列表條目= *新列表;'?爲什麼不'列出條目;'? – Nawaz

+0

你在哪裏'節點' – Anirudha

+0

增加了節點和隊列的接口。 – frankV

回答

0

將溶液非常簡單。我很尷尬,我以前沒有看到它。

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

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

}