c++ - 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()
?
解决方案
您需要替换Node
为node_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);
推荐阅读
- android - Android int/double 计算随机溢出
- memory-leaks - Xcode UIImage imageWithCGImage 内存泄漏
- python - Eliminate numbers between 1 and 100 present in list
- java - 用于 Java 的 ArraList.forEach 在 while 循环内无法正常工作
- android - 如何在 Android 中制作登录系统?
- javascript - 为什么我不能将标题背景设置为十六进制值?
- cakephp - Cakephp 从表中检索行数据
- android - 刷掉应用程序时通知不起作用
- swift - Firestore 数据库不存储数据
- rest - 独立(微)服务是个悖论吗?