2021-06-15 00:49:18 -07:00
---
id: 587d825a367417b2b2512c87
2021-08-02 23:05:44 +09:00
title: Creare una lista doppiamente concatenata
2021-06-15 00:49:18 -07:00
challengeType: 1
forumTopicId: 301626
dashedName: create-a-doubly-linked-list
---
# --description--
2021-08-02 23:05:44 +09:00
Tutte le liste concatenate che abbiamo creato finora sono liste semplicemente concatenate. Qui, creeremo una lista < dfn > doppiamente concatenata< / dfn > . Come suggerisce il nome, i nodi in una lista doppiamente concatenata hanno dei riferimenti ai nodi successivo e precedente nella lista.
2021-06-15 00:49:18 -07:00
2021-08-02 23:05:44 +09:00
Questo ci permette di attraversare la lista in entrambe le direzioni, ma richiede anche più memoria perché ogni nodo deve contenere un riferimento aggiuntivo al nodo precedente nella lista.
2021-06-15 00:49:18 -07:00
# --instructions--
2021-08-02 23:05:44 +09:00
Abbiamo fornito un oggetto `Node` e abbiamo avviato il nostro `DoublyLinkedList` . Aggiungiamo due metodi alla nostra lista doppiamente concatenata chiamati `add` e `remove` . Il metodo `add` dovrebbe aggiungere l'elemento dato alla lista mentre il metodo `remove` dovrebbe rimuovere tutte le occorrenze di un dato elemento nella lista.
2021-06-15 00:49:18 -07:00
2021-08-02 23:05:44 +09:00
Fai attenzione a gestire eventuali casi limite durante la scrittura di questi metodi, come le cancellazioni per il primo o l'ultimo elemento. Inoltre, la rimozione di qualsiasi elemento in una lista vuota dovrebbe restituire `null` .
2021-06-15 00:49:18 -07:00
# --hints--
2022-03-31 22:31:59 +05:30
La struttura dei dati `DoublyLinkedList` dovrebbe esistere.
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
return typeof test == 'object';
})()
);
```
2022-03-31 22:31:59 +05:30
Il `DoublyLinkedList` dovrebbe avere un metodo denominato `add` .
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
if (test.add == undefined) {
return false;
}
return typeof test.add == 'function';
})()
);
```
2022-03-31 22:31:59 +05:30
Il `DoublyLinkedList` dovrebbe avere un metodo denominato `remove` .
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
if (test.remove == undefined) {
return false;
}
return typeof test.remove == 'function';
})()
);
```
2022-03-31 22:31:59 +05:30
Inoltre, la rimozione di qualsiasi elemento da una lista vuota dovrebbe restituire `null` .
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
return test.remove(100) == null;
})()
);
```
2022-03-31 22:31:59 +05:30
Il metodo `add` dovrebbe aggiungere elementi alla lista.
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
test.add(5);
test.add(6);
test.add(723);
return test.print().join('') == '56723';
})()
);
```
2021-08-02 23:05:44 +09:00
Ogni nodo dovrebbe tenere traccia del nodo precedente.
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
test.add(50);
test.add(68);
test.add(73);
return test.printReverse().join('') == '736850';
})()
);
```
2021-08-02 23:05:44 +09:00
Il primo elemento dovrebbe essere rimovibile dall'elenco.
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
test.add(25);
test.add(35);
test.add(60);
test.remove(25);
return test.print().join('') == '3560';
})()
);
```
2021-08-02 23:05:44 +09:00
L'ultimo elemento dovrebbe essere rimovibile dall'elenco.
2021-06-15 00:49:18 -07:00
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
test.add(25);
test.add(35);
test.add(60);
test.remove(60);
return test.print().join('') == '2535';
})()
);
```
# --seed--
## --after-user-code--
```js
DoublyLinkedList.prototype = Object.assign(
DoublyLinkedList.prototype,
{
2021-07-09 21:23:54 -07:00
2021-06-15 00:49:18 -07:00
print() {
if (this.head == null) {
return null;
} else {
var result = new Array();
var node = this.head;
while (node.next != null) {
result.push(node.data);
node = node.next;
};
result.push(node.data);
return result;
};
},
printReverse() {
if (this.tail == null) {
return null;
} else {
var result = new Array();
var node = this.tail;
while (node.prev != null) {
result.push(node.data);
node = node.prev;
};
result.push(node.data);
return result;
};
}
});
```
## --seed-contents--
```js
var Node = function(data, prev) {
this.data = data;
this.prev = prev;
this.next = null;
};
var DoublyLinkedList = function() {
this.head = null;
this.tail = null;
// Only change code below this line
2021-07-09 21:23:54 -07:00
2021-06-15 00:49:18 -07:00
// Only change code above this line
};
```
# --solutions--
```js
// solution required
```