【问题标题】:How to generate unique ID in linked list?如何在链表中生成唯一 ID?
【发布时间】:2018-10-20 23:38:19
【问题描述】:

我目前正在课上学习如何在空闲时间使用链表。现在我只知道如何插入、显示和删除。我可以使用int age; 删除,但我认为如果我能够为链接列表中的每个数据生成一个更易于用户记住(用户友好)的唯一 ID 会更好,以便我可以删除通过它的 ID。

我想知道我是否可以在getInput(); 函数中生成唯一ID?

如果是,请给我一个提示如何做到这一点。谢谢!

struct list
{
    list *head, *tail;
    list *next;
}
class node
{
    private:
        std::string name; // Name
        int age; // Age in integer
        float height; // In meters

    public:
        node *next; // Pointer to next node
        node *head, *tail;
        node *start_ptr = NULL; // Start Pointer (root)
        node *temp;
        node *temp2;
        node *pNextValue;
        node* prev; // empty header
        node* current;
        void printList();
        void delete_end_node();
        void search();
        void sort_age();
        void deletebyAge();
    node()
        {
            head = NULL;
            tail = NULL;
        }


        void getInput()
        {
            temp = new node;
            cout << "ID: ";
            // This is where I want to generate unique ID
            cout << "Name: ";
            cin >> temp->name;
            cout << "Age: ";
            cin >> temp->age;
            cout << "Height: ";
            cin >> temp->height;

            cout<<"\n";
            temp->next = NULL; // Sets the node to be the last node
            if (start_ptr == NULL)
                start_ptr = temp;
            else
            {
                temp2 = start_ptr; // We know temp2 is not NULL - list not empty!
                while (temp2->next != NULL) // The loop will terminate when temp2
                    temp2 = temp2->next; // points to the last node in the list
                // Move to next link in chain
                temp2->next = temp; // Sets the pointer from that last node to point to the node that has just declared
            }
        } // End of getInput() function
}; //End of class

【问题讨论】:

    标签: c++ class linked-list


    【解决方案1】:

    在 C++ 中,对象的标识是它的地址。你可以使用node地址作为它的id,例如:

    class node
    {
        private:
            std::string name; // Name
            int age; // Age in integer
            float height; // In meters
    
        friend std::ostream& operator<<(std::ostream& s, node& n) {
            return s << "id:" << &n
                     << ' ' << "name:" << n.name
                     << ' ' << "age:" << n.age
                     << ' ' << "height:" << n.height
                     << '\n';
        }
        // the rest of your code...
    };
    

    然后像这样打印:

    node n;
    std::cout << n;
    

    或者,使用串行计数器:

    class node
    {
    private:
        std::string name; // Name
        int age; // Age in integer
        float height; // In meters
        unsigned const id;
    
        static unsigned object_counter;
    
    public:
        node()
            : id(++object_counter)
        {}
    
        friend std::ostream& operator<<(std::ostream& s, node& n) {
            return s << "id:" << n.id
                     << ' ' << "name:" << n.name
                     << ' ' << "age:" << n.age
                     << ' ' << "height:" << n.height
                     << '\n';
        }
        // the rest of your code...
    };
    
    unsigned node::object_counter = 0;
    

    【讨论】:

    • @beginner 为你添加了一个例子。
    • 但还缺一件事,它不方便用户,因为用户记住他们的ID非常复杂。
    • @beginner 您应该在问题中提及对 id 的要求。
    • 对不起,我忘了在问题中提及。
    • @beginner 为您添加了另一个示例。
    猜你喜欢
    • 2018-05-04
    • 2023-03-15
    • 2010-11-30
    • 2013-03-11
    • 2019-04-29
    • 1970-01-01
    • 2012-03-21
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多