C++ linked lists
C++ linked lists
我正在使用list类的修改版本进行作业。
class List
{
private:
class Node
{
public:
std::string _entry;
Node * _link;
Node(std::string entry, Node * link) : _entry(entry), _link(link)
{}
};
};
我想要完成的目标是进入一个列表后,我需要能够删除一个成员,所以如果我输入:
a
b
c
d
e
我需要能够删除c,而不影响其余部分。我的功能是:
bool deleteOneOf(const std::string & target)
{
Node * & del = _first;
Node * temp = _first;
if (contains(target))
{
del = find(temp, target);
del = del->_link;
}
delete temp;
return true;
}
else
{
return false;
}
}
, find函数为:
Node * & find(Node * & ptr, const std::string & target)
{
if (ptr == nullptr || ptr->_entry == target)
{
return ptr;
}
else
{
return find(ptr->_link, target);
}
}
我遇到的问题是,如果我输入C要删除,它不能正确地将B链接到D,所以C, D, E都被删除了,而不仅仅是C。所以输出是A B,而不是应该的A B D E。
deleteOneOf()中的代码对我来说有点混乱。我不清楚你想采用什么方法,但没必要这么复杂。
我假设_first
是指向列表的头指针。在这种情况下:
bool deleteOneOf(const std::string & target)
{
Node **del = &_first;
while (*del)
{
if ((*del)->_entry == target)
{
Node *ptr=*del;
(*del)=ptr->_link;
delete ptr;
return true;
}
del=&(*del)->_link;
}
return false;
}
除非我遗漏了您的某些要求,否则这应该是所有需要的。
相关文章:
- 重载Singly Linked List中的赋值运算符
- 提升图:当我的图使用 listS 作为 VertexList 时,如何调用深度优先搜索()?
- CMake:尝试在 Jenkins 构建机器上运行时出现"Linked Library"错误 (0xc0000135)
- Linked List in C++ InsertAtFront
- std::move in initializer lists
- Android OpenCV Linked CXX 共享库失败
- BGL:listS和index_map中的顶点
- 在C++中有条件地删除Singly Linked List中的元素
- 以listS为顶点容器的图上的astar_search
- 获取迭代器在基类型 "intrusive doubly linked list" 的指针字典中的位置
- c++ libconfig and lists
- 制作一个类以提供Lists接口
- 使用可变参数模板制作类似元组的编译时"linked-list"
- 为什么我的函数"missing argument lists"?
- Linked list class
- 'Corrupted double-linked list'内存错误 - OpenCV-Python 3.0.0 和 ARM 上的 Numpy
- C++ linked lists
- C++ OpenMPI linked-lists
- Single linked lists C++
- Double Linked Lists