c++ - for_each for Node 手动 c++

标签 c++ list foreach nodes

如何为 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 类:

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;

关于c++ - for_each for Node 手动 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34742084/

相关文章:

python - 如何返回字符串中所有大写字母索引的列表?

c# - 为什么这个 LINQ 表达式会破坏我的循环和转换逻辑?

c++ - 创建属于另一个使用 Qt Designer 制作的小部件的 Qt 小部件

c++ - 如何将鼠标移动转换为相机平移

c# - ASP GridView 列表

c# - 是什么导致 C# 中过多的匿名方法闭包 (c__DisplayClass1)?

c# - 使用 C# 对 List <string> 进行排序的正确方法是什么?

c++ - 在 C++ 中获取窗口标题

c++ - 使用 opengl 的立方体侧纹理

javascript - 谷歌地图 API : pan to markers from a list.