我如何能够访问双向链表中的嵌套结构
How might I be able to access a nested struct within a doubly linked list?
我正在尝试使用带有标题结构的双向链表。我相信标头结构只是应该保存列表中已创建的结构数量以及第一个和最后一个节点。现在我看到两个问题。一个是将标头结构连接到后续结构,第二个是访问我在列表节点(哨兵(中的嵌套结构。如果有人能阐明我可能做错了什么,将不胜感激。代码如下。
#include <iostream>
using namespace std;
typedef struct sentry sentry;
struct stud{
string name;
char grade;
};
struct slist {
int length;
sentry *first;
sentry *last;
};
struct sentry {
slist *list;
sentry *next;
sentry *prev;
stud *student;
};
int main(int argc, char** argv) {
slist list;
sentry *n;
n = new sentry;
// Full initialization of the header node.
list.first = n;
list.last = n;
list.length = 0;
n->prev = NULL;
n->next = NULL;
n->list = list->last;
n->student->name = "test";
n->student->grade = 'A';
cout << n->student->name << 'n';
cout << n->student->grade << 'n';
return 0;
}
为什么不修改结构哨兵以不包含slist *list
?为什么sentry
和slist
之间需要双向关系?这使得维护、读取和更新哨兵列表中的值变得乏味。没关系,解除堆上的内存以避免内存泄漏。为什么不让slist
(标题(只有有关sentry
的信息。通过这种方式,您可以消除sentry
和slist
之间双重绑定的复杂性。
struct sentry {
sentry *next;
sentry *prev;
stud *student;
};
相关文章:
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 用C#中的并集模拟C++嵌套结构
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- Lambda 捕获、初始值设定项和嵌套结构
- 使用g++静态初始化带有命名标签的嵌套C++结构
- 重载嵌套结构/类的流插入运算符
- C++将结构传递给函数以访问嵌套结构
- C++ 在外部使用嵌套结构实现类函数
- 嵌套结构,从由空行分隔的文件中读取数据
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 受保护的嵌套结构不能用作派生外部类中的返回类型?
- C++中的嵌套结构
- 使用嵌套结构初始化并集
- C++ 中的嵌套结构函数
- C++中匿名类型的嵌套结构
- c++ 嵌套结构映射找不到成员
- C 嵌套结构上的 g++ 编译错误
- 不能使用嵌套结构中的联合元素作为 scanf() 的参数来存储所需的值
- 如何将值添加到嵌套结构中,该结构在C++中有两个指针