简体   繁体   English

如何将 append 和 C++ 传递到链表末尾的 Node* 参数?

[英]How to append and pass a Node* parameter at the end of the linked list with C++?

  • I'm trying to create the append() member function which takes a pointer to a PatientList object as parameter and returns nothing.我正在尝试创建append()成员 function,它将指向PatientList object 的指针作为参数并且不返回任何内容。
  • The node passed as a parameter is appended to the end of the linked list.作为参数传递的节点附加到链表的末尾。
  • For this problem a node of a list is a List* object.对于这个问题,列表的一个节点是List* object。

I come here to try to get some help.我来这里是想寻求帮助。

[EDITED] [编辑]

If there is something that is not understandable tell me and I will try to update my post as soon as possible.如果有什么不明白的地方告诉我,我会尽快更新我的帖子。

First I have 2 classes with member functions:首先,我有 2 个具有成员函数的类:

#include <iostream>
#include <string>

class Patient {
   public:
    Patient();
    Patient(std::string name);
    ~Patient();
    std::string get_name(void);

   private:
    std::string _name;

   protected:
};

Patient::Patient() : _name("Patient") {}

Patient::Patient(std::string name) : _name(name) {}

Patient::~Patient() {}

std::string Patient::get_name(void) { return _name; }

class PatientList {
private:
    Patient *_content;
    PatientList *_next;
    PatientList *_prev;

public:
    PatientList() = default;
    PatientList(Patient *patient);
    ~PatientList();
    bool isEnd(void);
    void append(PatientList *node);
    Patient *getContent();
    PatientList *getNext();
    void setNext(PatientList *next);
    void setContent(Patient *content);
    void PrintList();
};

PatientList::PatientList(Patient *content)
    : _content(content), _next(nullptr) {}
PatientList::~PatientList() {}
bool PatientList::isEnd() {
    if (_next == nullptr)
        return true;
    else
        return false;
}
Patient *PatientList::getContent() { return _content; }
PatientList *PatientList::getNext() { return _next; }
void PatientList::setNext(PatientList *next) { this->_next = next; }
void PatientList::setContent(Patient *content) { this->_content = content; }

// Function for display list
void PatientList::PrintList() {
    PatientList *temp = this;
    std::cout << "Patients : ";
    while (temp) {
        // For verify if between 2 nodes there are something nullptr.
        if (temp->_next == nullptr) {
            std::cout << "NULLPTR ";
        }
        std::cout << temp->_content->get_name() << " ";
        temp = temp->_next;
    }
    std::cout << std::endl;
}

void PatientList::append(PatientList *node) {
      // or PatientList* newNode = new PatientList(node->_content); I hesitate...
    PatientList *newNode = new PatientList;
    newNode->_content = node->_content;
    PatientList *temp = this;
    while (temp->_next != NULL) {
        temp = temp->_next;
    }
    temp->_next = node;
    newNode->_prev = temp;  // maybe i dont need it ?
}

int main() {
    Patient k1("name1");
    Patient k2("name2");
    Patient k3("name3");
    Patient k4("name4");
    Patient k5("name5");

    PatientList P1(&k1);
    PatientList P2(&k2);
    PatientList P3(&k3);
    PatientList P4(&k4);
    PatientList P5(&k5);

    P1.append(&P2);
    P1.append(&P3);
    P1.append(&P4);
    P1.append(&P5);

    P1.PrintList();
}

Output: Output:

Patients : name1 name2 name3 name4 NULLPTR name5

Output request: Output 请求:

Patients : name1 name2 name3 name4 name5

So, I have a new problems:所以,我有一个新问题:

  • There is something null before the last node.在最后一个节点之前有一些 null。 I dont understand why.我不明白为什么。
  • Where does the problem come from?问题从何而来? append , PrintList , or somewhere else? appendPrintList或其他地方?

[EDITED] [编辑]

Thank you in advance.先感谢您。

A possible implementation for a PatientList class (basically a wrap-up of the comments above): PatientList class 的可能实现(基本上是上述评论的总结):

  • It uses smart pointers.它使用智能指针。
  • It only has two public methods (at the moment), empty() , and void append(Patient patient) .它只有两个公共方法(目前), empty()void append(Patient patient)
  • It adds a convenience tail_ pointer, useful for appending nodes.它添加了一个方便的tail_指针,可用于附加节点。
  • Users append patients as patient_list.append(Patient{ "name" });用户 append patients as patient_list.append(Patient{ "name" }); . .
    They don't know anything about the list implementation.他们对列表实现一无所知。
  • Patient , PatientNode , and PatientList are different entities. PatientPatientNodePatientList是不同的实体。

[Demo] [演示]

#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]

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

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