2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d8251367417b2b2512c61
|
2020-12-16 07:37:30 +00:00
|
|
|
title: 使用链接列表中的节点
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
|
|
|
videoUrl: ''
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: work-with-nodes-in-a-linked-list
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
您将在计算机科学中遇到的另一个常见数据结构是<dfn>链表</dfn> 。链表是数据元素的线性集合,称为“节点”,每个数据元素指向下一个。链表中的每个<dfn>节点都</dfn>包含两个关键信息: `element`本身和对下一个`node`的引用。想象一下你在康加舞线上。你的手掌握在线下的下一个人身上,你身后的人就会抓住你。你可以直接看到这个人,但是他们阻挡了前方其他人的视线。一个节点就像一个康加舞线上的人:他们知道自己是谁,他们只能看到下一个人,但他们并不知道前方或后方的其他人。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
在我们的代码编辑器中,我们创建了两个节点, `Kitten`和`Puppy` ,我们手动将`Kitten`节点连接到`Puppy`节点。创建`Cat`和`Dog`节点并手动将它们添加到该行。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
您的`Puppy`节点应该具有对`Cat`节点的引用。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(Puppy.next.element === 'Cat');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
您的`Cat`节点应该具有对`Dog`节点的引用。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(Cat.next.element === 'Dog');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
var Node = function(element) {
|
|
|
|
this.element = element;
|
|
|
|
this.next = null;
|
|
|
|
};
|
|
|
|
var Kitten = new Node('Kitten');
|
|
|
|
var Puppy = new Node('Puppy');
|
|
|
|
|
|
|
|
Kitten.next = Puppy;
|
|
|
|
// Only change code below this line
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
// solution required
|
|
|
|
```
|