有人能解释一下吗??这是某种结构构造函数吗? (链接列表)

时间:2012-08-28 23:55:30

标签: constructor struct linked-list

所以我们一直在学习CS课程中的链表,我理解这个概念就好了。但是,在查看示例程序时,我的教授指定他提供的代码与我们在课堂上展示的代码不同。

以下是示例中的代码:

        struct itemType
        {
            string store_name, item_name;
            int item_number, quantity; 
            float price;
        } ;

        struct node
        {
             itemType item;
             node *next;    //  
             **node (itemType it, node* n=NULL)
             {
                 item=it;
                 next=n;
             }**
         };

我不明白为什么他在节点结构中调用节点并让它采用两个参数(代码中的部分包含在'**'中)。它看起来像构造函数或其他东西。我用Google搜索或阅读的链接列表上的每个示例都没有那么一点代码!

但是你们给我的任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:1)

该示例是C ++代码。 C ++中的类是结构概念的扩展,实际上关于类和结构之间的唯一区别是在结构中所有成员默认是公共的,而在类中它们默认是私有的。这是因为C ++的历史起点是“C With Classes” - 只需用struct替换struct,添加“public:”,它看起来就不那么陌生了:

    struct itemType
    {
        string store_name, item_name;
        int item_number, quantity; 
        float price;
    } ;

    class node
    {
    public:
         itemType item;
         node *next; 
         node (itemType it, node* n=NULL) // Constructor
         {
             item=it;
             next=n;
         }
     };