【发布时间】:2013-12-16 19:01:24
【问题描述】:
我需要一个函数来根据其位置从链表中搜索项目,将信息保存在变量中并删除它。例如,我想删除列表中的第五项并将其内容保存到 int&number;和字符串&文本; 我的列表仅在一个方向链接。 我想我已经设法找到了,但删除它有点困难。
private:
struct List_cell{
unsigned number;
string text;
List_cell *next;
};
List_cell *list_first_;
.
bool LinkedList::find_and_remove(unsigned& position, unsigned& count, unsigned& found_number, string& found_text){
List_cell *current = list_first_;
if(current == nullptr){
return false;
}
else{
while(current != nullptr){
count++;
current = current->next;
if(count == position){
found_number = current->number;
found_text = current->text;
//here should be the deleting i think
return true;
}
}
}
return false;
}
我是否正确完成了所有操作以及如何删除?
【问题讨论】:
-
嗯,您应该记住链接到您要删除的对象的对象。使用代码,您可以在检查之前松开要更改的链接...
-
我建议您先在纸上写下所需的步骤,然后(重新)编码解决方案。编码后,在调试器中逐行执行代码以确保其正常工作。
标签: c++ linked-list