【问题标题】:c++ remove similar nodes linked listc++ 删除相似节点链表
【发布时间】:2013-09-10 22:35:45
【问题描述】:

对于家庭作业,我需要删除该数字传入的所有相似节点。例如,如果我在列表中

3 5 5 4

5's 将从链表中删除,我将结束

3 4

我们不允许使用这个类的std库,这是头文件

    namespace list_1
{
    class list
    {
    public:
        // CONSTRUCTOR
        list( );
        // postcondition: all nodes in the list are destroyed.
        ~list();
        // MODIFICATION MEMBER FUNCTIONS
        //postcondition: entry is added to the front of the list
        void insert_front(const int& entry);
        //postcondition: entry is added to the back of the list
        void add_back(const int& entry);
        // postcondition: all nodes with data == entry are removed from the list
        void remove_all(const int& entry);
        // postcondition: an iterator is created pointing to the head of the list
        Iterator begin(void);

        // CONSTANT MEMBER FUNCTIONS
        // postcondition: the size of the list is returned
        int size( ) const;
    private:
        Node* head;
    };

}

我可以理解如何删除列表的前面和后面。但是由于某种原因,我无法绕过列表并删除所有传入的数字。任何有帮助!谢谢

编辑以包含 Node.h

#pragma once

namespace list_1
{
    struct Node
    {
        int data;
        Node *next;

        // Constructor
        // Postcondition: 
        Node (int d);
    };
}

【问题讨论】:

  • 在一张纸上画一幅画:每个节点都用一个方框来表示。将每个框拆分为成员变量。从每个成员绘制一个箭头,表示指向它所指向的框的指针。您可以用手指跟随绘图上的箭头来浏览列表:只需让程序做同样的事情!
  • “相似”还是“相同值”?了解其中的区别很重要。例如,32 可能与 31 和 33“相似”,但并不相同。或者“阅读”这个词(过去时)听起来与“红色”的颜色非常相似。但拼写不同。
  • 对不起,如果它是相同的值,那么它应该被删除。目前正在尝试绘制图片。
  • 我注意到您有一个名为 head 的 Node*。能否请您发布 Node 类的标题?
  • 你能遍历列表吗?您可以删除列表中间的节点吗?你能比较两个节点吗?你能有一个指向一个节点的指针并在你从那里迭代到最后时将它保留在那里吗?开发这些并单独测试它们,然后将它们组合在一起就完成了。

标签: c++ singly-linked-list


【解决方案1】:

有两种方法可以做到这一点。第一个是遍历列表并删除节点。这很棘手,因为要做到这一点,您必须保留指向前一个节点的指针,以便您可以更改其 next 值。 删除节点的代码如下所示(假设current 是当前节点,prev 是前一个节点)

Node* next = current->next;
delete current;
prev->next = next;

维护对前一个节点的引用可能有点乏味,所以这里有另一种方法。在这种方法中,您实际上创建了一个新列表,但不插入 data 等于 entry 的节点。

代码可能有点像这样

void list::remove_all(const int &entry)
{
    Node* newHead = NULL;
    Node* newTail = NULL;
    Node* current = head;

    // I'm assuming you end your list with NULL
    while(current != NULL)
    {
        // save the next node in case we have to change current->next
        Node* next = current->next;
        if (current->data == entry)
        {
            delete current;
        }
        else
        {
            // if there is no head, the set this node as the head
            if (newHead == NULL)
            {
                newHead = current;
                newTail = current;
                newTail->next = NULL; // this is why we saved next
            }
            else
            {
                // append current and update the tail
                newTail->next = current;
                newTail = current;
                newTail->next = NULL; // also why we saved next
            }
        }
        current = next; // move to the next node
    }
    head = newHead; // set head to the new head
}

注意:我没有对此进行测试,我只是在脑海中输入了它。确保它有效。 =)

希望这会有所帮助! ;)

【讨论】:

  • 谢谢,我还没有测试过这段代码,但至少这有助于我理解它。在我看来,我试图保持指向前一个的指针。但是制作副本对我来说更有意义
  • 起初我打算使用指向前一个的方法来编写代码,但我很快意识到这样做会简单得多。
猜你喜欢
  • 2019-05-10
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 2013-08-30
  • 2016-01-02
  • 2019-05-10
  • 2016-03-02
相关资源
最近更新 更多