【问题标题】:Deleting item in one way linked list单向链表删除项目
【发布时间】: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


【解决方案1】:

编程引理:所有问题都可以通过额外的间接层来解决:

bool LinkedList::find_and_remove( unsigned& position,
                                  unsigned& count,
                                  unsigned& found_number,
                                  string& found_text )
{
    List_cell **work = &list_first_;
    while(*work != nullptr) {
        if ( ++count == position ) {
            List_cell *tmp = *work;
            *work = (*work)->next;
            found_number = tmp->number;
            found_test = tmp->text;
            delete tmp;
            return true;
        }
        work = &(*work)->next;
    }
    return false;
}

【讨论】:

  • 哦.. 我认为它不起作用。当我使用它时,它似乎总是删除第一个!这真的有效还是我做错了什么?
  • 哎呀...对不起。忘记了一条线。当我编写代码而不测试它时会发生这种情况:)
  • 顺便说一句:如果你在家庭作业中使用它,你的教授几乎肯定不会相信你写的 :)
  • 为什么将work 声明为List_cell **?现在work 每次被调用时都必须遵守。我也看不到任何低级的好处,因为它是重新分配地址与重新分配地址。还是指针所指向的大小决定了指针的大小?
  • 啊我现在明白了*work 相当于previous->next 所以*work = (*work)->next 是取消链接找到的节点。但是work = &(*work)->next 需要在else 内。否则,如果找到的节点位于列表的末尾,则 **work = null(*work)->next(**work).next 未定义。
【解决方案2】:

您已经找到要删除的节点,所以现在您只需将节点 before 它链接到节点 after 它。因此,您需要一个指向该节点之前的节点的指针。

你需要修改你的代码为

if head is null, return false
initialize counter to 0
create two pointers: p as head and q as head->next
while q != null do
    if counter == position do <- q is pointing to the node you need to remove
        store the info from q
        p->next = q->next <- this is where the node gets removed
        return true
    q = q->next
    p = p->next
return false

或者递归执行:(并不总是建议,但需要更少的代码)

deleteNode(head, position):
    if head == null 
        return null
    if position == 0:
        return head->next
    head->next = deleteNode(head->next, position - 1)
    return head

【讨论】:

    【解决方案3】:

    您需要将节点的next 指针存储在已删除节点之前,并将其附加到已删除单元格之后的节点。

    所以你需要一个前一个指针

    List_cell* previous;
    

    在你的 while 循环中

    count++;
    previous = current;
    current = current->next;
    if(count == position){
        found_number = current->number;
        found_text = current->text;
        previous->next = current->next;
        delete current;
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2011-08-18
      • 2017-05-19
      • 1970-01-01
      相关资源
      最近更新 更多