简体   繁体   English

我可以通过这种方式创建链接列表吗?

[英]Can i create a linked list in this way?

struct node
{
    int data;
    node *link;
} n1, n2;
n1.data = 1;
n1.link = &n2;  //in the link pointer of n1 store the address of n2
n2.data = 2;

Is the above code correct? 上面的代码正确吗? What command should I write to print the data of the node whose address is stored in the link of n1. 我应该写什么命令来打印地址存储在n1链接中的节点的数据。

Is the above code correct? 上面的代码正确吗?

It will compile, and run, but it would make sense to set n2.link = NULL; 它将编译并运行,但是设置n2.link = NULL;有意义的n2.link = NULL; so that your other code can find where the end of the list is. 这样您的其他代码就可以找到列表的末尾位置。

What command should I write to print the data of the node whose address is stored in the link of n1. 我应该写什么命令来打印地址存储在n1链接中的节点的数据。

printf("%d\n", n1.link->data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM