【问题标题】:deleting values less than a given value in a doubly linked list在双向链表中删除小于给定值的值
【发布时间】: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


【解决方案1】:

在查找要删除的节点时,您没有正确遍历列表。如果列表为空,代码将崩溃。如果循环遇到不早于指定年份的节点,则循环提前结束。删除节点时,代码没有正确考虑该节点位于列表前面或后面的可能性。

试试这个:

void deleteLessThanYear(int y)
{
    Node* current = head; //position current at head

    if (!head)
    {
        cout << "No nodes in list" << endl << endl;
        return;
    }

    do
    {
        Node* currentNext = current->getNext();
        if (current->getYear() < y)
        {
            Node* currentPrev = current->getPrev();
            if (currentPrev) currentPrev->setNext(currentNext);
            if (currentNext) currentNext->setPrev(currentPrev);
            if (current == head) head = currentNext;
            delete current;
        }
        current = currentNext;
    }
    while (current);
}

【讨论】:

    【解决方案2】:

    您的实施中有几个错误:

    1. if( head == NULL) 条件中没有return; 语句。如果你在达到这个条件后不返回,你最终会运行current-&gt;getYear(),导致Segmentation Fault cannot access memory at address
    2. 不检查while(current-&gt;getYear() &gt; y) 条件内的current != NULL 条件。如果列表的所有值都大于y,则您最终会遍历整个列表,从而到达末尾,即NULL 指针,这将再次导致Segmentation Fault
    3. 如果head 的年份值y,你还没有处理这种情况。
    4. 您停在第一个节点的年份 > y,因此只删除下一个节点,但您提到要删除“所有”歌曲。您需要遍历完整列表并删除所有具有年份值 y 的节点。

    完整的实现需要修改如下:

    void deleteLessThanYear(int y)
    {
        // Handling the case if head has year > y
        while(head != NULL && head->getYear() > y){
            head = head->getNext();
        }
    
        if( head == NULL)
        {
            cout << "No nodes in list" << endl << endl;
            return; // return
        }else{
            head->setPrev(NULL);
        }
    
        Node* current = head; //position current at modified head
    
        while(current != NULL){
            if(current->getYear() <= y){
                Node* currentPrev = current->getPrev();
                Node* currentNext = current->getNext();
                currentPrev->setNext(currentNext);
                // You need to check here if currentNext exists or not
                if(currentNext != NULL){
                    currentNext->setPrev(currentPrev);
                }
                current = currentPrev;
            }
            current = current->getNext();
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多