链表帮助C++; 我在C++中把数组显示和显示到链表中有困难。一般来说,我在使用链表时遇到很多麻烦

链表帮助C++; 我在C++中把数组显示和显示到链表中有困难。一般来说,我在使用链表时遇到很多麻烦,c++,c++11,linked-list,C++,C++11,Linked List,这是我的密码。它只显示第一个数字。(应显示19 21 17 22 33) #包括 使用名称空间std; 结构节点{ 智力年龄; 节点*下一步; }; 无效显示(节点*t) { 节点*temp=t; 下一个年龄; 节点*列表=新节点; 列表=空; 节点*temp2=列表; } 对于(int i=0;iage=age[i]; temp2->next=NULL; if(List==NULL) List=temp2; } 显示(列表); 系统(“暂停”); 返回0; } 这里发生了很多事情。我建议您阅读

这是我的密码。它只显示第一个数字。(应显示19 21 17 22 33)

#包括
使用名称空间std;
结构节点{
智力年龄;
节点*下一步;
};
无效显示(节点*t)
{
节点*temp=t;
下一个年龄;
节点*列表=新节点;
列表=空;
节点*temp2=列表;
}
对于(int i=0;i<5;i++)
{
temp2=新节点;
temp2->age=age[i];
temp2->next=NULL;
if(List==NULL)
List=temp2;
}
显示(列表);
系统(“暂停”);
返回0;
}

这里发生了很多事情。我建议您阅读一些教程,以帮助巩固对指针和链表的理解

为了回答您的问题,让我们逐步了解正在发生的事情:

main
函数中,动态分配
节点
对象,并将
列表
指向该对象。然后,将
List
设置为
NULL
。当您将
节点
指针
temp2
初始化到
列表
时,
temp2
也指向
NULL

因为
temp2=NULL
,所以在循环时跳过以下

for
循环的第一次迭代中,将
temp2
重新分配给新创建的对象,然后将该
节点的
年龄设置为
年龄[0]
。当我们到达您的
if
语句时,
List=NULL
因此我们输入条件语句并将
List
设置为
temp2
其中
temp2->age=age[0]

对于
For
循环的每一次额外迭代,
List
不再等于
NULL
,因此您永远不会再次输入
if
语句<代码>列表
仍然指向
节点
,其中
age=age[0]


当您使用display函数时,假设它是通过链接列表循环编写的,则
列表
指针是一个列表,其中有一个节点
age=19
。假设您有一个包含多个有效节点的链表,那么您的
display()
函数不会对它们进行迭代。

以下面的代码为例。请注意,您必须在程序结束时删除所有创建的节点,以避免内存泄漏

#include <iostream>
#include <cstddef>

using namespace std;

struct node {
    int age;
    node* next;
};

void display(node* list) {
    while (list != NULL) {
        cout << list->age << ' ';
        list = list->next;
    }
}

node* create(int age) {
    node* t = new node;
    t->age = age;
    t->next = NULL;
    return t;
}

void add(node* list, node* t) {
    while (list->next != NULL) {
        list = list->next;
    }
    list->next = t;
}

void release(node* list) {
    while (list != NULL) {
        node* t = list;
        list = list->next;
        delete t;
    }
}

int main() {
    int arr[5] = { 19, 21, 17, 22, 33 };

    node* list = create(arr[0]);
    for (int i = 1; i < 5; i++) {
        node* t = create(arr[i]);
        add(list, t);
    }

    display(list);

    release(list);

    system("PAUSE");
    return 0;
}
#包括
#包括
使用名称空间std;
结构节点{
智力年龄;
节点*下一步;
};
无效显示(节点*列表){
while(list!=NULL){
下一个年龄;
}
}
节点*创建(整数年龄){
node*t=新节点;
t->年龄=年龄;
t->next=NULL;
返回t;
}
无效添加(节点*列表,节点*t){
while(列表->下一步!=NULL){
列表=列表->下一步;
}
列表->下一步=t;
}
无效释放(节点*列表){
while(list!=NULL){
节点*t=列表;
列表=列表->下一步;
删除t;
}
}
int main(){
int-arr[5]={19,21,17,22,33};
节点*列表=创建(arr[0]);
对于(int i=1;i<5;i++){
节点*t=create(arr[i]);
添加(列表,t);
}
显示(列表);
发布(列表);
系统(“暂停”);
返回0;
}

List=NULL后接
节点*temp2=列表。现在有两个指针指向
NULL
和通过
node*List=new node分配的节点泄漏。首先,我强烈建议在互联网上搜索“C++链表示例”。通过StackOverflow可能比调试器快得多。可视化对链表很有帮助。把清单画在一张纸上。把链接画进去。在插入、删除或以其他方式修改列表时,逐步修改列表。对所采取的步骤做好记录,并将这些记录用作代码的基础。然后按照您编码的说明绘制相同的列表。如果你不能,那么你就有一个bug,并且可能是一个非常好的想法,你错在哪里了。一个非常有用的技巧是在节点中添加一个构造函数,如果你不提供不同的链接,它会将
next
设置为
NULL
。这样你就不会忘记设置
next
。你在很多地方都没有正确使用
next
。当您应该更改指向列表中最后一个节点的
next
指针时,您可以更改头指针。
#include <iostream>
#include <cstddef>

using namespace std;

struct node {
    int age;
    node* next;
};

void display(node* list) {
    while (list != NULL) {
        cout << list->age << ' ';
        list = list->next;
    }
}

node* create(int age) {
    node* t = new node;
    t->age = age;
    t->next = NULL;
    return t;
}

void add(node* list, node* t) {
    while (list->next != NULL) {
        list = list->next;
    }
    list->next = t;
}

void release(node* list) {
    while (list != NULL) {
        node* t = list;
        list = list->next;
        delete t;
    }
}

int main() {
    int arr[5] = { 19, 21, 17, 22, 33 };

    node* list = create(arr[0]);
    for (int i = 1; i < 5; i++) {
        node* t = create(arr[i]);
        add(list, t);
    }

    display(list);

    release(list);

    system("PAUSE");
    return 0;
}