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

时间:2022-01-28 07:16:59

In this function (part of a bigger contact book program I am working on) it displays an item in a linked list, or in my case a contact. I would like to get the position of this item in the linked list, in order to delete the contact and add it back to the linked list.

在这个函数中(我正在处理的一个更大的联系簿程序的一部分),它在链表中显示一个项目,或者在我的情况下显示一个联系人。我想在链接列表中获取此项目的位置,以便删除联系人并将其添加回链接列表。

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";
        }
}

The actual result I get right now is just the contact information (Variables from the linked list)

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

1 个解决方案

#1


0  

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

if found idx has the index

如果发现idx有索引

#1


0  

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

if found idx has the index

如果发现idx有索引