4.6 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d825a367417b2b2512c87 | Criar uma lista duplamente encadeada | 1 | 301626 | create-a-doubly-linked-list |
--description--
Todas as listas que criamos até agora são listas encadeadas com apenas uma direção. Aqui, criaremos uma lista duplamente encadeada. Como o nome sugere, os nós de uma lista duplamente encadeada têm referências para o próximo nó e para o nó anterior na lista.
Isto nos permite atravessar a lista em ambas as direções, mas também requer que mais memória seja usada, pois cada nó deve conter uma referência adicional ao nó anterior na lista.
--instructions--
Fornecemos um objeto Node
e começamos nossa DoublyLinkedList
. Vamos adicionar dois métodos à nossa lista de duplamente encadeada chamados add
e remove
. O método add
deve adicionar o elemento dado à lista, enquanto o método remove
deve remover todas as ocorrências de um determinado elemento na lista.
Tenha cuidado ao lidar com possíveis casos nas extremidades da lista ao escrever estes métodos, tais como exclusões do primeiro ou do último elemento. Além disso, remover qualquer item em uma lista vazia deve retornar null
.
--hints--
A estrutura de dados DoublyLinkedList
deve existir.
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
return typeof test == 'object';
})()
);
DoublyLinkedList
deve ter um método chamado add
.
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
if (test.add == undefined) {
return false;
}
return typeof test.add == 'function';
})()
);
DoublyLinkedList
deve ter um método chamado remove
.
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
if (test.remove == undefined) {
return false;
}
return typeof test.remove == 'function';
})()
);
Remover um item em uma lista vazia deve retornar null
.
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
return test.remove(100) == null;
})()
);
O método add
deve adicionar itens À lista.
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';
})()
);
Cada nó deve manter um registro do nó anterior.
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';
})()
);
O primeiro item deve poder ser removido da lista.
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';
})()
);
O último item deve poder ser removido da lista.
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--
DoublyLinkedList.prototype = Object.assign(
DoublyLinkedList.prototype,
{
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--
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
// Only change code above this line
};
--solutions--
// solution required