Este documento discute estruturas de dados, incluindo estruturas lineares como pilhas, filas e listas, e não-lineares como tabelas de espalhamento e árvores. Ele se concentra em listas encadeadas, definindo uma classe Node para representar cada nó e uma classe LinkedList para implementar operações como inserção e remoção de nós em qualquer posição da lista.
3. Estruturas
Valor
(value)
Node
Próximo
(next)
› Cada nó é uma estrutura
personalizada, com o valor
armazenado e a referência
do objeto seguinte
Valor
(value)
Node
Próximo
(next)
Valor
(value)
Node
Próximo
(next)
null
› Se não existir próximo,
aponta-se para nulo
Relembrando...
4. class Node {
constructor (value) {
this.value = value
this.next = null
}
}
JavaScript
Estrutura de um Nó
Relembrando...
13. Inserir Nó na Listafirst
next
next
1
2
3
0
next
insert ('MK X', 2)
14. Inserir Nó na Listafirst
next
next
1
2
3
0
next
insert ('MK X', 2)
let target = this.first
for (let i = 0; i < index - 1; i++)
target = target.next
15. Inserir Nó na Listafirst
next
next
next
next
inserted.next = target.next
target.next = inserted
16. remove (index) {
this.length--
if (index === 0) {
let removed = this.first
this.first = removed.next
return removed
} else {
let target = this.first
for (let i = 0; i < index - 1; i++)
target = target.next
let removed = target.next
target.next = removed.next
return removed.value
}
}
JavaScript
Remover Nó da Lista