不会显示多个值

时间:2021-01-04 16:05:47

There is no error but I was expecting 10 values but I only got one. The below is my create and display function.

没有错误,但我期待10个值,但我只有一个。以下是我的创建和显示功能。

void create()
{
int random;
for (int i = 0; i < 10; i++)
{
    struct node *new_node, *current;
    new_node = new node;
    random = randomNum();
    new_node->data = random;
    new_node->next = NULL;
    if (start == NULL)
    {
        start = new_node;
        current = new_node;
        new_node = NULL;
    }
    else
    {
        current->next = new_node;
        current = new_node;
    }
 }
}

void display()
{
struct node *new_node;
new_node = start;
while (new_node != NULL)
{
    cout << new_node->data << "->";
    new_node = new_node->next;
}
}

What do I need to change?

我需要改变什么?

2 个解决方案

#1


0  

Try the following line

请尝试以下行

current = new_node;    
current->next = new_node;

Because in your case there is no value of current pointer so how can you update its next. First of all update/set the current then update/set its next.

因为在你的情况下没有当前指针的值,所以你如何更新它的下一个。首先更新/设置当前然后更新/设置其下一个。

if (start == NULL)
    {
        start = new_node;
        current = new_node;
        current->next = NULL;
        new_node = NULL;
    }
    else
    {
        current = new_node;
        current->next = new_node;

    }

#2


0  

The problem with the compiler message is that the compiler is unable to determine whether the variable current used in the else statement was early initialized.

编译器消息的问题是编译器无法确定else语句中使用的变量current是否已提前初始化。

If it is a warning you may ignore it.

如果是警告,您可以忽略它。

Or you can rewrite the function the following way

或者您可以通过以下方式重写该功能

void create()
{
    const int N = 10;

    node **current = &start;

    while ( *current ) current = &( *current )->next;

    for ( int i = 0; i < N; i++ )
    {
        *current = new node;
        ( *current )->data = randomNum();
        ( *current )->next = nullptr;
        current = &( *current )->next;
    }
 }

The body of the for loop can be also written like (I assume that the data member data precedes the data member next in the node definition. Otherwise exchange the initializers.

for循环的主体也可以写成(我假设数据成员数据在节点定义中的下一个数据成员之前。否则交换初始化程序。

*current = new node { randomNum(), nullptr };
current = &( *current )->next;

#1


0  

Try the following line

请尝试以下行

current = new_node;    
current->next = new_node;

Because in your case there is no value of current pointer so how can you update its next. First of all update/set the current then update/set its next.

因为在你的情况下没有当前指针的值,所以你如何更新它的下一个。首先更新/设置当前然后更新/设置其下一个。

if (start == NULL)
    {
        start = new_node;
        current = new_node;
        current->next = NULL;
        new_node = NULL;
    }
    else
    {
        current = new_node;
        current->next = new_node;

    }

#2


0  

The problem with the compiler message is that the compiler is unable to determine whether the variable current used in the else statement was early initialized.

编译器消息的问题是编译器无法确定else语句中使用的变量current是否已提前初始化。

If it is a warning you may ignore it.

如果是警告,您可以忽略它。

Or you can rewrite the function the following way

或者您可以通过以下方式重写该功能

void create()
{
    const int N = 10;

    node **current = &start;

    while ( *current ) current = &( *current )->next;

    for ( int i = 0; i < N; i++ )
    {
        *current = new node;
        ( *current )->data = randomNum();
        ( *current )->next = nullptr;
        current = &( *current )->next;
    }
 }

The body of the for loop can be also written like (I assume that the data member data precedes the data member next in the node definition. Otherwise exchange the initializers.

for循环的主体也可以写成(我假设数据成员数据在节点定义中的下一个数据成员之前。否则交换初始化程序。

*current = new node { randomNum(), nullptr };
current = &( *current )->next;