2012-08-28 60 views
0

所以我们一直在学习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

这是一个构造函数。结构可以像类一样拥有构造函数;他们的行为完全一样。 – templatetypedef

回答

1

这个例子是C++代码。 C++中的类是struct概念的扩展,实际上关于类和结构之间的唯一区别在于,在结构中所有成员默认都是公共的,而在类中它们默认是私有的。这是由于C++作为“C with Classes”的历史开始 - 只需将类替换为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; 
     } 
    }; 
相关问题