You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++链表作业问题:bool函数如何同时返回删除状态与节点值

Hey Sara, let's break down this problem and fix your getBack function step by step!

Fixing the List::getBack Member Function

First, let's clarify the core challenge and the issues in your current code:

How to Return Both Status and Node Value

You’re already on the right track here! The int &key reference parameter is exactly how you’ll pass the node’s value back to the caller. The boolean return value only needs to signal whether the operation succeeded (e.g., returning false if the list is empty or only has one node, since there’s no "node before the tail" to delete).

Critical Issues in Your Current Code Snippet

From what you’ve shared, two big problems stand out:

  • Unnecessary new Node calls: You’re allocating new nodes for temp and prev, but immediately reassigning those pointers to head. This creates memory leaks—those new nodes will never be deleted! Just declare pointer variables without new for traversal.
  • Incomplete traversal logic: You need to correctly locate the node right before the tail, plus handle edge cases like empty lists or single-node lists.

Corrected Implementation

Here’s a full, working version of the function with comments explaining each step:

// Assume your Node struct looks like this:
// struct Node {
//     int data;
//     Node* next;
//     Node(int val) : data(val), next(nullptr) {}
// };

bool List::getBack(int &key) {
    // Edge case: No valid node to delete (empty list or single node)
    if (head == nullptr || head->next == nullptr) {
        return false;
    }

    Node* prev = nullptr;
    Node* current = head;

    // Traverse until current is the node right before the tail
    // (current->next will be the tail node)
    while (current->next->next != nullptr) {
        prev = current;
        current = current->next;
    }

    // Pass the target node's value back via the reference parameter
    key = current->data;

    // Remove the current node from the list
    if (prev == nullptr) {
        // Only two nodes in the list: update head to point to the tail
        head = current->next;
    } else {
        // Bypass the current node to link prev directly to the tail
        prev->next = current->next;
    }

    // Free the memory of the deleted node to avoid leaks
    delete current;

    return true;
}

Key Details to Remember

  • Using References for Output: The int &key lets you modify the caller’s variable directly—this is how you "return" the node’s value alongside the boolean success status.
  • Memory Management: Never use new unless you’re creating a new node for the list. Always delete nodes you remove to prevent memory leaks.
  • Edge Case Handling: Always check for empty lists or lists too small to perform the operation—these are the most common sources of crashes or unexpected behavior.

内容的提问来源于stack exchange,提问作者Sara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:43:10