for_each for Node manually c++

for_each for Node manually c++

本文关键字:for c++ manually each Node      更新时间:2023-10-16

如何为Node和List的实现实现for_each?我相信这不是很长的代码,可以分享一下他的知识吗?我需要将它作为模板来实现还是仅仅作为内部函数来实现。谢谢你的帮助。

class Node {
    int data;
    Node *next;
  public:
    Node() {}
    void SetData(int aData) { data = aData; }
    void SetNext(Node *aNext) {
        next = aNext;
    }
    int Data() { return data; }
    Node* Next() { return next; }
};
// List class
class List {
    Node *head;
  public:
    List() { head = nullptr; }
    void Append(int data){
        // Create a new node
        Node *newNode = new Node();
        newNode->SetData(data);
        newNode->SetNext(nullptr);
        // Create a temp pointer
        Node *tmp = head;
        if ( tmp != nullptr ) {
        // Nodes already present in the list
        // Parse to end of list
        while ( tmp->Next() != nullptr) {
            tmp = tmp->Next();
        }
        // Point the last node to the new node
        tmp->SetNext(newNode);
        }
        else {
        // First node in the list
        head = newNode;
        }
    }
    void Delete(int data){
        // Create a temp pointer
        Node *tmp = head;
        // No nodes
        if ( tmp == nullptr ) return;
        // Last node of the list
        if ( tmp->Next() == nullptr ) {
            delete tmp;
            head = nullptr;
        }
        else {
            // Parse thru the nodes
            Node* prev;
            do {
            if ( tmp->Data() == data ) break;
                prev = tmp;
                tmp = tmp->Next();
            } while ( tmp != nullptr );
            // Adjust the pointers
            if (tmp->Next()!=nullptr) prev->SetNext(tmp->Next());
            else prev->SetNext(nullptr);
            // Delete the current node
            if (tmp!=nullptr) delete tmp;
        }
    }
};

编辑:这是迭代器的用法:

for (List<Pair, CompareFunction>::myIterator it = this->_pairs.begin();
            it != this->_pairs.end(); ++it) {
                Pair cur_pair = *it;
                if (cur_pair.first == key) {
                    this->_pairs.Delete(cur_pair);
                    this->_size--;
                }
            }

这是Pair Class作为另一个模板类中的子类:

template <class KeyType, class ValueType, class CompareFunction = std::less<KeyType> >
    class MtmMap {
    public:
        class Pair {
        public:
            Pair():first(KeyType()){} ////////////////////
            Pair(const KeyType& key, const ValueType& value)
                : first(key), second(value) {}
            const KeyType first;
            ValueType second;
            ~Pair() = default;

在我们的具体案例中,KeyType和ValueType都作为int.

运行

将以下代码添加到您的List类中:

class List{
    ...
    class myIterator
    {
    public:
        typedef myIterator self_type;
        typedef Node* pointer;
        myIterator(pointer ptr) : ptr_(ptr) { }
        self_type operator++() {
            self_type i = *this;
            if (ptr_) ptr_ = ptr_->Next();
            return i;
        }
        int operator*() { if (ptr_ ) return ptr_->Data(); else return 0; }
        bool operator==(const self_type& rhs) {
            if (!ptr_ && !rhs.ptr_) return true;
            return (ptr_ && rhs.ptr_ && rhs.ptr_->Data()==ptr_->Data());
        }
        bool operator!=(const self_type& rhs) {
            return !(*this==rhs);
        }
    private:
        pointer ptr_ = nullptr;
    };
    myIterator begin() { return myIterator(head); }
    myIterator end() { return myIterator(nullptr); }
};

然后,您可以使用正常的迭代器,如以下示例所示:

List l;
l.Append(2);
l.Append(5);
l.Append(7);
l.Append(11);
for (int i: l) std::cout << i << std::endl;
//or
for (myIterator it=l.begin(); it!=l.end(); ++it) std::cout << *it << std::endl;