我应该如何在 C++ 中初始化链表?
How should I initialize linked list in C++?
我有一个数组,我必须将其初始化为一个列表
我尝试做什么
#include <stdio.h>
#include <string.h>
struct data_t
{
unsigned int id_;
char name_ [50];
};
struct node_t
{
node_t * next_;
data_t data_;
};
void initialize(node_t **, const char **, const unsigned int);
int main()
{
node_t * first = NULL;
const char * data [3] = {"Alpha", "Bravo", "Charlie"};
initialize(&first, data, 3);
return 0;
}
void initialize(node_t ** head, const char ** data, const unsigned int n) {
node_t * current = NULL;
node_t * previous = NULL;
for (size_t i = 0; i < n; i++)
{
current = new node_t;
current->next_ = previous;
current->data_.id_ = i+1;
strcpy(current->data_.name_, data[i]);
if (i == 1)
{
*head = previous;
previous->next_ = current;
} else {
previous = current;
}
}
};
next_
只是循环并在 2 个值之间变化。我尝试了许多不同的选择,但没有任何效果。请帮忙。
为什么会这样?
在 'first' 与 'not first' 的情况下,您需要做一些特殊的事情,您知道这一点,但错了。
- 在第一个 (i==0) 上,您需要设置 head 以便调用者获得指向第一个节点的指针
- 在后续的上,您必须将先前的下一个指针设置为指向当前。对于第一个没有先验
另外你将 current->next
设置为指向 previous
,那也是错误的,这导致了你的循环
这就是您所需要的
for (size_t i = 0; i < n; i++)
{
current = new node_t;
current->next_ = NULL; <<<======
current->data_.id_ = i + 1;
strcpy(current->data_.name_, data[i]);
if (i == 0)
*head = current;
else
previous->next_ = current;
previous = current;
}
因为你不能使用std::string
s,我建议你在data_t
中添加一个可以复制char
数组的构造函数:
struct data_t {
data_t(unsigned id, const char* name) :
id_{id} // can be copied automatically
{
// the char array needs to be copied manually:
std::strncpy(name_, name, sizeof name_ - 1);
name_[sizeof name_ - 1] = '[=10=]';
}
unsigned int id_;
char name_[50];
};
这样,您的 initialize
函数可以简化为
// last in `data`, first in the list:
void initialize(node_t*& head, const char** data, const unsigned int n) {
for (unsigned int i = 0; i < n; i++) {
// create a new `node_t` with `next_` pointing at the current `head`
// `data_` will be initialized by calling the added constructor
// assign the returned `node_t` to `head`
head = new node_t{head, {i + 1, data[i]}};
}
}
或
// first in `data`, first in the list:
void initialize(node_t*& head, const char** data, const unsigned int n) {
for (unsigned int i = n; i > 0; --i) {
head = new node_t{head, {i, data[i - 1]}};
}
}
请注意,initialize
通过引用 head
(&
) 来简化调用:
int main() {
node_t* first = nullptr;
const char* data[3] = {"Alpha", "Bravo", "Charlie"};
initialize(first, data, 3); // not &first here
}
我有一个数组,我必须将其初始化为一个列表 我尝试做什么
#include <stdio.h>
#include <string.h>
struct data_t
{
unsigned int id_;
char name_ [50];
};
struct node_t
{
node_t * next_;
data_t data_;
};
void initialize(node_t **, const char **, const unsigned int);
int main()
{
node_t * first = NULL;
const char * data [3] = {"Alpha", "Bravo", "Charlie"};
initialize(&first, data, 3);
return 0;
}
void initialize(node_t ** head, const char ** data, const unsigned int n) {
node_t * current = NULL;
node_t * previous = NULL;
for (size_t i = 0; i < n; i++)
{
current = new node_t;
current->next_ = previous;
current->data_.id_ = i+1;
strcpy(current->data_.name_, data[i]);
if (i == 1)
{
*head = previous;
previous->next_ = current;
} else {
previous = current;
}
}
};
next_
只是循环并在 2 个值之间变化。我尝试了许多不同的选择,但没有任何效果。请帮忙。
为什么会这样?
在 'first' 与 'not first' 的情况下,您需要做一些特殊的事情,您知道这一点,但错了。
- 在第一个 (i==0) 上,您需要设置 head 以便调用者获得指向第一个节点的指针
- 在后续的上,您必须将先前的下一个指针设置为指向当前。对于第一个没有先验
另外你将 current->next
设置为指向 previous
,那也是错误的,这导致了你的循环
这就是您所需要的
for (size_t i = 0; i < n; i++)
{
current = new node_t;
current->next_ = NULL; <<<======
current->data_.id_ = i + 1;
strcpy(current->data_.name_, data[i]);
if (i == 0)
*head = current;
else
previous->next_ = current;
previous = current;
}
因为你不能使用std::string
s,我建议你在data_t
中添加一个可以复制char
数组的构造函数:
struct data_t {
data_t(unsigned id, const char* name) :
id_{id} // can be copied automatically
{
// the char array needs to be copied manually:
std::strncpy(name_, name, sizeof name_ - 1);
name_[sizeof name_ - 1] = '[=10=]';
}
unsigned int id_;
char name_[50];
};
这样,您的 initialize
函数可以简化为
// last in `data`, first in the list:
void initialize(node_t*& head, const char** data, const unsigned int n) {
for (unsigned int i = 0; i < n; i++) {
// create a new `node_t` with `next_` pointing at the current `head`
// `data_` will be initialized by calling the added constructor
// assign the returned `node_t` to `head`
head = new node_t{head, {i + 1, data[i]}};
}
}
或
// first in `data`, first in the list:
void initialize(node_t*& head, const char** data, const unsigned int n) {
for (unsigned int i = n; i > 0; --i) {
head = new node_t{head, {i, data[i - 1]}};
}
}
请注意,initialize
通过引用 head
(&
) 来简化调用:
int main() {
node_t* first = nullptr;
const char* data[3] = {"Alpha", "Bravo", "Charlie"};
initialize(first, data, 3); // not &first here
}