如何用C++在链表末尾追加并传递Node* 参数?

gt0wga4j  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(67)
  • 我正在尝试创建append()成员函数,该函数将指向PatientList(类)的指针作为参数,但不返回任何内容。
  • 做为参数传递的节点会附加至链接清单的结尾。

我做了一些东西,但我不知道如何测试我的代码简单,如果这个代码是正确的。所以我来这里尝试获得一些帮助。如果有什么不明白告诉我,我会尝试更新我的职位尽快。
首先我有2个类的成员函数:
第一个
这里是append()成员函数:

void            PatientList::append(PatientList    *node)
{
        Patient   patient;
        node = new PatientList(&patient);

        /* 1. create and allocate node */
        PatientList *newNode = new PatientList(&patient);
        PatientList *last = node; /* used in step 5*/
        
        /* 2. assign data to the node */
        newNode->setContent(node->getContent());

        /* 3. set next pointer of new node to null as its the last node*/
        newNode->setNext(NULL);
        
        /* 4. if list is empty, new node becomes first node */
        if (node == NULL)
        {
            node = newNode;
            return;
        }
        
        /* 5. Else traverse till the last node */
        while (last->getNext() != NULL) // or maybe (last->isEnd()) ?
                last = last->getNext();
        
        /* 6. Change the next of last node */
        last->setNext(newNode);
        return;
}

我还试了另一种办法:

void PatientList::append(PatientList *node)
{
    Patient patient;
    PatientList *tmp = new PatientList(&patient);

    if (node == nullptr)
        return;
    if (node->getContent == nullptr) {
        while (tmp->isEnd() == false)
            tmp = tmp->getNext();
        tmp->setNext(node);
        return;
    }

    while (tmp->isEnd() == false) {
        tmp = tmp->getNext();
    }
    
    tmp->setNext(node);
}

所以,我想知道:

  • 我的append()成员函数是否正确?
  • 我不知道自己到底做了什么,你能帮我弄明白吗?
  • 我如何测试这个函数?

我读了其他类似的帖子,我与他们和其他网站与谷歌,但我不认为我理解得很好,或者我没有设法纠正我的问题。
先谢谢你。

jaxagkaj

jaxagkaj1#

PatientList类的一个可能实现(基本上是上述注解的总结):

  • 它使用智能指针。
  • 它只有两个公共方法(目前),empty()void append(Patient patient)
  • 它添加了一个方便的tail_指针,用于追加节点。
  • 用户将患者追加为patient_list.append(Patient{ "name" });

他们对列表实现一无所知。

  • PatientPatientNodePatientList是不同的图元。

Demo(https://godbolt.org/z/v14YMEhhn)

#include <iostream>  // cout
#include <memory>  // make_unique, unique_ptr
#include <string>

struct Patient {
    std::string name_;
    Patient() : name_{ "Patient" } {}
    explicit Patient(std::string name) : name_{ std::move(name) } {}
};

class PatientList {
private:
    struct PatientNode {
        Patient patient_;
        std::unique_ptr<PatientNode> next_;
    };
    std::unique_ptr<PatientNode> head_;
    PatientNode* tail_;
public:
    bool empty() {
        return not head_;
    }
    void append(Patient patient) {
        if (empty()) {
            head_ = std::make_unique<PatientNode>(std::move(patient));
            tail_ = head_.get();
        } else {
            tail_->next_ = std::make_unique<PatientNode>(std::move(patient));
            tail_ = tail_->next_.get();
        }
    }
    friend std::ostream& operator<<(std::ostream& os, const PatientList& l) {
        os << "[";
        for (auto node_ptr{ l.head_.get() }; node_ptr; node_ptr = node_ptr->next_.get()) {
            os << (node_ptr == l.head_.get() ? "" : ", ") << node_ptr->patient_.name_;
        }
        return os << "]";
    }
};

int main() {
    PatientList pl{};
    pl.append(Patient{ "p3" });
    pl.append(Patient{ "p2" });
    pl.append(Patient{ "p1" });
    std::cout << pl << "\n";
}

// Outputs: [p3, p2, p1]

相关问题