首页 > 解决方案 > Doubly Linked List with struct c++

问题描述

I have a struct named fruit_t:

struct fruit_t {
    char fruit_name[MAX_LENGTH];                                            // name of fruit
    float quantity;                                                 // in lbs
    float price;                                                    // price tag of the fruit
    float new_quantity;
};

struct node_t{
    struct Node* next;
    struct Node* prev;      //constructor
    fruit_t fruit;          //data declarations
};

struct Node* head;              // global variable - pointer to head node
struct Node* create_new_Node (fruit_t fruit);

My question is: did I use the correct syntax for declaring create_new_Node()?

标签: c++

解决方案


您需要替换Nodenode_t

struct node_t{
    struct node_t* next;
    struct node_t* prev;
    fruit_t fruit;
};

struct node_t* head = nullptr;
struct node_t* create_new_Node(fruit_t fruit);

话虽如此,struct关键字在类型声明之外的 C++ 中是可选struct的,不像在 C 中,在struct任何地方都需要关键字 astruct被引用。因此,上面的内容可以简化为:

struct node_t{
    node_t* next;
    node_t* prev;
    fruit_t fruit;
};

node_t* head = nullptr;
node_t* create_new_Node(fruit_t fruit);

推荐阅读