[英]Accessing struct within struct C++
I'm writing a C++ programm which has to work with linked list.我正在编写一个必须使用链表的 C++ 程序。 But I can't figure out how can I access structure which is in another structure.但我不知道如何访问另一个结构中的结构。
#include <cstddef>
#include "list.hpp"
using std::size_t;
struct list {
struct node {
double val;
node* prev;
node* next;
};
node* head = nullptr;
node* tail = nullptr;
size_t size = 0;
};
Can you explain me how it works?你能解释一下它是如何工作的吗? I have a method, but I don't know how I can use this structur in this method.我有一个方法,但我不知道如何在这个方法中使用这个结构体。
void push_back(list& l, double elem) {
node *new_node = new node(elem);
if (l.head==null) {
l.head = new_node;
}
node *curent = l.head;
while (curent) {
if (!curent->next) {
curent->next = new_node;
}
cur = cur->next;
}
}
Thank you.谢谢你。
in this code , you have a doubly linked list在这段代码中,你有一个双向链表
i'll try to explain the code of the push_back function .我将尝试解释 push_back 函数的代码。
at the beginning we have void push_back(list& l, double elem) , l is your current LinkedList whish you want to add a new element to it in queue, elem is the value of your new element .一开始我们有 void push_back(list& l, double elem) , l 是你当前的 LinkedList 你想在队列中添加一个新元素,elem 是你的新元素的值。
if (l.head==null) {
l.head = new_node;
}
if your linkedList is empty , we add the new element如果您的链接列表为空,我们添加新元素
exemple1 : empty LinkedList示例 1 : 空链表
if the linkedList is not empty如果链表不为空
this a simple code这是一个简单的代码
node *curent = l.head; // the current node is pointed to the head of the LinkedList
while (curent->next != null) { // while current->next is not equal to null
curent=curent->next ; // step forward to the next node
}
curent->next =new_node ; // add the new node to the queue of the linkedList
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.