【发布时间】:2021-03-27 22:01:28
【问题描述】:
我正在编写一个模拟音乐播放列表的代码。我的一个功能应该删除给定年份之前的所有歌曲。但是我在让我的代码正常工作时遇到了一些问题。该函数不会删除节点,当我在运行该函数后使用函数打印列表时,它完全没有变化。有人可以帮忙吗?
功能
void deleteLessThanYear(int y)
{
Node* current = head; //position current at head
if( head == NULL)
{ cout << "No nodes in list" << endl << endl;}
while(current->getYear() > y)
{
current = current->getNext();
if (current->getYear() <= y)
{
Node* currentPrev = current->getPrev();
Node* currentNext = current->getNext();
currentPrev->setNext(currentNext);
currentNext->setPrev(currentPrev);
delete current;
}
}
.main函数调用
cout << endl;
cout << "------------------------------" << endl;
cout << "DELETE LESS THAN YEAR" << endl;
playlist->deleteLessThanYear(2005);
playlist->printList();
【问题讨论】:
-
这看起来像是通过在调试器中单步执行代码以查看执行流程何时不再与预期流程匹配的最佳解决方案。 (保持列表简短以便调试。)
标签: c++ linked-list