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

C++98双向链表deleteAcc函数仅传字符串参数的作用域问题求解

双向链表删除节点疑问:仅用单个字符串参数能否实现目标功能?

我基于Account类和Node类实现了双向链表,其余函数运行正常,唯独bool deleteAcc(const string name1)函数存在问题。该函数需求为:接收用户输入的账户名字符串,找到对应Account节点并删除后返回true,未找到则返回false。

当前遇到的问题:

  • 原代码中deleteAcc函数无法访问main函数内的head指针,存在作用域问题
  • 尝试将head声明为全局变量后,仍未得到预期输出

特此询问:仅使用单个字符串参数能否实现该函数的功能?

原代码

#include <iostream>
#include "Account.h"
#include <string>
#include "Node.h"
using namespace std;

bool deleteAcc(const string name1);

int main(){

    string name;
    int k;
    cout << "How many accounts do you want to enter? ";
    cin >> k;

    Node* head = NULL;
    //Node* tail = NULL;

    for (int i = 0; i < k; i++) {
        string name;
        double balance;

        cout << "Enter account name: ";
        cin >> name;

        cout << "Enter account balance: ";
        cin >> balance;
    
        Account account(name, balance);

        Node* newNode = new Node(account);

        if (head == NULL) {
            // The list is empty, so set both head and tail to the       
            // new node
            head = newNode;
            // tail = newNode;
        } else {
            // The list is not empty, so add the new node to the  
            // tail
            newNode->setNext(head);
            //newNode->setPrevious(head); // Update previous pointer 
            // of newNode to point to the previous last node (i.e.,   
            // tail)
            head = newNode;
        }
    }

    // Print the list
    cout << "Account balances:" << endl;
    cout << endl;

    Node* currentNode = head;

    while (currentNode != NULL) {
        cout << currentNode->getData() << endl;
        currentNode = currentNode->getNext();
    }

    cout << "Enter the account name you want to delete: ";
    cin >> name;
    deleteAcc(name);



    // Deallocate memory
    currentNode = head;

    while (currentNode != NULL) {
        Node* nextNode = currentNode->getNext();
        delete currentNode;
        currentNode = nextNode;
    }

    return 0;
}

bool deleteAcc(const string name1)
{
    Node* currentNode = head;
    Node* previousNode = NULL;

    while (currentNode != NULL){
        if (currentNode->getData().getName() == name1) {
            if (previousNode == NULL) {
                // The node to be deleted is the head node
                head = currentNode->getNext();
            }   
            else {
            // The node to be deleted is in the middle of the list
            previousNode->setNext(currentNode->getNext());
            }  

        delete currentNode;
        return true;
        }

        // Update the previous node and move to the next node
        previousNode = currentNode;
        currentNode = currentNode->getNext();
    }

    // The account was not found in the list
    return false;
}

尝试全局变量的代码

Node* head = NULL; 

int main(){
    // functions and variables here
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 20:42:46