【问题标题】:Insert node at specified custom class iterator position在指定的自定义类迭代器位置插入节点
【发布时间】:2020-10-13 15:16:25
【问题描述】:

我试图在指定位置插入一个节点,问题是插入发生在我想要的位置之后的一个位置。例如:

在list.insert(list.begin()++, 100) 插入 100 到 1,2,3,4,5,6,7 结果:

1,2,100,3,4,5,6,7

我想过递减迭代器,然后插入,但这将是一种解决方法,我的代码中有一些有缺陷的逻辑,但我看不到它在哪里。

插入函数

iterator insert(iterator position, const T& value) {

        Node* newNode = new Node(value);

        if (position == List<T>::iterator(head_)) {
            newNode->next_ = head_;
            head_ = newNode;
        }
        else if (!position.iNode->next_) {
            position.iNode->next_ = newNode;
        }
        else {

            Node* tmp = new Node(value);
            tmp->next_= position.iNode->next_;
            position.iNode->next_ = tmp;
        }

        return position;
    }

迭代器类

class iterator
    {
    public:

        Node* iNode;
        iterator(Node* head): iNode(head){ }
        ~iterator() {}


        T& operator*() {
            return  iNode -> value_;
        }
    
        iterator& operator++() {
            iNode = iNode->next_;
            return *this;
        }

        iterator operator++(int ignored) {
            iNode = iNode->next_;
            return *this;
        }

        //! Is this iterator pointing at the same place as another one?
        bool operator== (const iterator& it) const {
            return this->iNode == it.iNode;
        }

        //! Is this iterator pointing at a different place from another?
        bool operator!= (const iterator& it) const {
            return this->iNode != it.iNode;
        }
    };

节点类

class Node {
    public:
        Node(T value) : value_(value) {}

        T value_;
        Node* next_;
        Node* prev_;

        Node* head_;
        Node* tail_;
    };

【问题讨论】:

  • 编译您的代码并在调试器中打开您的程序。在iterator insert(iterator position, const T&amp; value) { 处设置断点,启动程序并逐行执行代码。但是您可以在这里看到position.iNode-&gt;next_ = newNode; 和position.iNode-&gt;next_ = tmp;,您的代码在position 之后插入了新元素。
  • 我知道这是我的代码的预期行为,我只是不知道如何修复它。
  • 你用笔和纸把它修好。你没有算法。你首先要为这个问题开发一个算法。绘制列表元素并将它们连接起来。添加一个新的列表元素并了解如何重新连接它们。你不适合在代码中工作。

标签: c++ class linked-list iterator nodes


【解决方案1】:

插入中的问题:

iterator insert(iterator position, const T& value) {

        Node* newNode = new Node(value);

        if (position == List<T>::iterator(head_)) {
            newNode->next_ = head_;
            head_ = newNode;
        }
        else if (!position.iNode->next_) {
            position.iNode->next_ = newNode;
        }
        else {
            Node* curr = head_; 
            while (curr->next_ != position.iNode) 
                {
                    curr = curr->next_;
                }

            newNode->next_ = curr->next_;
            curr->next_ = newNode;
        }

        return position;
    }

在迭代器位置之后插入一个新节点。但是你需要在新节点之前插入它。

顺便说一句,我在自己的 List 类中测试过它。如果出现问题,只需在我的帖子中添加评论。

更新:实际上,我当时只修复了部分问题。现在的逻辑是:

  1. 在迭代器之前从头到节点迭代。
  2. 将新节点连接到我们在第一步中迭代到的下一个节点。
  3. 将我们在第一步中迭代到的节点连接到新节点。

【讨论】:

  • 感谢您的帮助。我正在尝试,但它似乎不起作用。之后它仍在将元素添加到一个位置。我会尝试更多地调试并找出原因。
猜你喜欢
  • 2021-02-08
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 2013-02-17
  • 1970-01-01
相关资源
最近更新 更多