【发布时间】:2020-06-20 15:32:02
【问题描述】:
关于数据结构中链表的问题(C语言)
假设我在列表中有 4 个元素,就像
(地址)[数据 |指向下一个块的指针]
像这样 (252) [5 | 272]--> (272)[8 |286]--> (286)[11 | 296]--> (296)[15|NULL]
struct node
{
int data;
struct node* next;
};
struct node* head; //global variable
这是关于箭头控制的问题 如果我想从头部遍历到第4个元素,我可以这样说吗?
(252) [5 | 272]--> (272)[8 |286]--> (286)[11 | 296]--> (296)[15|NULL]---X
// let's suppose I stored 1st element in a variable and already linked them together
struct node* temp1 = (struct node*) malloc(sizeof(struct node*));
temp1->data = 5;
temp1->next = 272;
.
.
.
我可以这么说吗
temp1->next; // value is 272
temp1->next->data; // data is 8
temp1->next->next; // value is 286
temp1->next->next->data; //data is 11
temp1->next->next->next; // value is 296
temp1->next->next->next->data; // value is 15
temp1->next->next->next->next; // value is NULL
是否可以使用多个箭头直接遍历链表而不是使用循环?
【问题讨论】:
-
为什么不能?
-
是吗?如果你问是否完成了;是的。
-
我看到您正在为指针而不是节点本身节省空间! This question 有同样的问题。阅读 Andreas 的改编答案。
标签: c linked-list