[英]How to append and pass a Node* parameter at the end of the linked list with C++?
append()
member function which takes a pointer to a PatientList
object as parameter and returns nothing.append()
成员 function,它将指向PatientList
object 的指针作为参数并且不返回任何内容。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:所以,我有一个新问题:
append
, PrintList
, or somewhere else? append
、 PrintList
或其他地方? [EDITED] [编辑]
Thank you in advance.先感谢您。
A possible implementation for a PatientList
class (basically a wrap-up of the comments above): PatientList
class 的可能实现(基本上是上述评论的总结):
empty()
, and void append(Patient patient)
.empty()
和void append(Patient patient)
。tail_
pointer, useful for appending nodes.tail_
指针,可用于附加节点。patient_list.append(Patient{ "name" });
patient_list.append(Patient{ "name" });
. Patient
, PatientNode
, and PatientList
are different entities. Patient
、 PatientNode
和PatientList
是不同的实体。#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.