从方法中获得链接列表中的位置

Getting position in linked list from method

本文关键字:列表 链接 位置 方法      更新时间:2023-10-16

在此功能中(我正在处理的较大的联系书程序的一部分)它在链接列表中显示一个项目,或者在我的情况下是联系人。我想在链接列表中获得此项目的位置,以删除联系人并将其添加回链接列表。

void display_contact(std::string first, std::string last)
{
        bool found;
        node *curr = head;
        found = false;
        while (curr != NULL & !found)
        {
            if (curr->firstName == first)
            {
                found = true;
            }
            else if (curr->lastName == last)
            {
                found = true;
            }
            else
            {
                curr = curr->next;
            }
        }
        if (found)
        {
            std::cout << "First Name: " << curr->firstName << "n";
            std::cout << "Last Name: " << curr->lastName << "n";
            std::cout << "Phone Number: " << curr->phoneNumber << "n";
            std::cout << "Editing contacts is coming soon.n";
        }
        else
        {
            std::cout << "n" << first << " " << last << " was not found.n";
        }
}

我现在得到的实际结果只是联系信息(链接列表的变量)

node *curr = head;
found = false;
int idx = -1;
while (curr != NULL & !found)
{
    idx++;
    ....
 }

如果找到IDX具有索引