简体   繁体   English

C ++删除类似节点的链表

[英]c++ remove similar nodes linked list

For a homework assignment I need to remove all similar nodes that the number passed into. 对于家庭作业,我需要删除该数字传入的所有类似节点。 For example if I have on the list 例如,如果我在清单上

3 5 5 4 3 5 5 4

the 5's will be removed from the linked list and I will end with 5将被从链接列表中删除,我将以

3 4 3 4

we are not allowed to use the std library for this class and here is the header file 我们不允许对此类使用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;
    };

}

I can understand how to remove the front, and the back of the list. 我可以理解如何删除列表的前面和后面。 But for some reason I can't wrap my head around going through the list and removing all of the number that is passed in. Anything helps! 但是由于某种原因,我无法全神贯注地浏览列表并删除所有传入的数字。任何帮助! Thanks 谢谢

edited to include Node.h 编辑以包含Node.h

#pragma once

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

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

There are two ways of doing this. 有两种方法可以做到这一点。 The first is to iterate through the list and remove the nodes. 首先是遍历列表并删除节点。 This is tricky because to do that you have to keep a pointer to the previous node so you can change its next value. 这很棘手,因为要做到这一点,您必须保留指向上一个节点的指针,以便可以更改其next值。 The code for removing a node would look like this (assume current is the current node and prev is the previous node) 对于删除节点看起来像这样的代码(假设current是当前节点prev是以前的节点)

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

Maintaining a reference to the previous node can be a bit tedious though, so here is another way to do it. 维护对前一个节点的引用可能会有些乏味,因此这是另一种方法。 In this method, you essentially create a new list but don't insert Nodes who's data is equal to entry . 在此方法中,您实质上是创建一个新列表,但不插入data等于entry Node。

The code might look a little like this 该代码可能看起来像这样

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
}

Note: I didn't test this, I just typed it up off the top of my head. 注意:我没有对此进行测试,只是在脑海中打了一下。 Make sure it works. 确保它可以工作。 =) =)

Hope this helps! 希望这可以帮助! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM