- 我正在尝试创建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()成员函数是否正确?
- 我不知道自己到底做了什么,你能帮我弄明白吗?
- 我如何测试这个函数?
我读了其他类似的帖子,我与他们和其他网站与谷歌,但我不认为我理解得很好,或者我没有设法纠正我的问题。
先谢谢你。
1条答案
按热度按时间jaxagkaj1#
PatientList
类的一个可能实现(基本上是上述注解的总结):empty()
和void append(Patient patient)
。tail_
指针,用于追加节点。patient_list.append(Patient{ "name" });
。他们对列表实现一无所知。
Patient
、PatientNode
和PatientList
是不同的图元。Demo(https://godbolt.org/z/v14YMEhhn)